2009-09-10 10 views
6

Mam długi ciąg (wiele akapitów), który muszę podzielić na listę ciągów linii. Ustalenie, co sprawia, że ​​„linia” opiera się na:Dzielenie ciągu bez linii łamie się na listę linii z maksymalną liczbą kolumn

  • Liczba znaków w wierszu jest mniejsza lub równa X (gdzie X oznacza stałą liczbę kolumn na line_)
  • OR, istnieje nowy wiersz w oryginalnym łańcuchu (który wymusi utworzenie nowej "linii")

Wiem, że mogę to zrobić algorytmicznie, ale zastanawiałem się, czy python ma coś, co poradzi sobie z tym przypadkiem. zawijanie słów ciągiem znaków:

I, przy okazji, outpu t wiersze muszą być łamane na granicach słów, a nie na granicach znaków.

Oto przykład wejścia i wyjścia:

Wejście:

"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." 

wyjściowa:

"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." 

Odpowiedz

12

EDIT

Co szukasz jest textwrap, ale to tylko część nie rozwiązanie pełna jednym. Aby wziąć pod uwagę newline, musisz to zrobić:

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

Dobra odpowiedź! Tak też myślałem. Ale co z "Wilsonem, a" po pustej linii? –

+0

@Andrei, zaktualizowałem swoją odpowiedź jeszcze lepszym rozwiązaniem. –

+0

Fajnie! Ale teraz wygląda na brudny. : P –

Powiązane problemy