2009-09-10 9 views
6

J'ai une longue chaîne (plusieurs paragraphes) que je dois diviser en une liste de chaînes de caractères. La détermination de ce qui constitue une « ligne » est basée sur:Division d'une chaîne sans saut de ligne dans une liste de lignes avec un nombre de colonnes maximum

  • Le nombre de caractères dans la ligne est inférieure ou égale à X (où X est un nombre fixe de colonnes par line_)
  • OU, il y a une nouvelle ligne dans la chaîne d'origine (qui forcera une nouvelle « ligne » à créer.

Je sais que je peux le faire algorithmiquement mais je me demandais si python a quelque chose qui peut gérer ce cas. Il est essentiellement mot-emballage une chaîne

Et, en passant, le outpu Les lignes t doivent être brisées sur les limites des mots et non sur les limites des caractères.

Voici un exemple d'entrée et de sortie:

Entrée:

"Within eight hours of Wilson's outburst, his Democratic opponent, former-Marine Rob Miller, had received nearly 3,000 individual contributions raising approximately $100,000, the Democratic Congressional Campaign Committee said. 

Wilson, a conservative Republican who promotes a strong national defense and reining in the size of government, won a special election to the House in 2001, succeeding the late Rep. Floyd Spence, R-S.C. Wilson had worked on Spence's staff on Capitol Hill and also had served as an intern for Sen. Strom Thurmond, R-S.C." 

Sortie:

"Within eight hours of Wilson's outburst, his" 
"Democratic opponent, former-Marine Rob Miller," 
" had received nearly 3,000 individual " 
"contributions raising approximately $100,000," 
" the Democratic Congressional Campaign Committee" 
" said." 
"" 
"Wilson, a conservative Republican who promotes a " 
"strong national defense and reining in the size " 
"of government, won a special election to the House" 
" in 2001, succeeding the late Rep. Floyd Spence, " 
"R-S.C. Wilson had worked on Spence's staff on " 
"Capitol Hill and also had served as an intern" 
" for Sen. Strom Thurmond, R-S.C." 

Répondre

12

EDIT

Ce que vous cherchez est textwrap, mais ce n'est qu'une partie de la solution et non l'un complète. Pour prendre en compte le saut de ligne, vous devez faire ceci:

from textwrap import wrap 
'\n'.join(['\n'.join(wrap(block, width=50)) for block in text.splitlines()]) 

>>> print '\n'.join(['\n'.join(wrap(block, width=50)) for block in text.splitlines()]) 

Within eight hours of Wilson's outburst, his 
Democratic opponent, former-Marine Rob Miller, had 
received nearly 3,000 individual contributions 
raising approximately $100,000, the Democratic 
Congressional Campaign Committee said. 

Wilson, a conservative Republican who promotes a 
strong national defense and reining in the size of 
government, won a special election to the House in 
2001, succeeding the late Rep. Floyd Spence, 
R-S.C. Wilson had worked on Spence's staff on 
Capitol Hill and also had served as an intern for 
Sen. Strom Thurmond 
+0

Bonne réponse! C'est comme ça que j'ai pensé le faire aussi. Mais ce qui est avec le "Wilson, un" après la ligne blanche? –

+0

@Andrei, j'ai mis à jour ma réponse avec une solution encore meilleure. –

+0

Cool! Mais maintenant, il semble malpropre. : P –

Questions connexes