2009-09-10 25 views
6

Tôi có chuỗi dài (nhiều đoạn văn) mà tôi cần chia thành danh sách các chuỗi dòng. Việc xác định những gì làm cho một "dòng" được dựa trên:Tách chuỗi không có dấu ngắt dòng vào danh sách các dòng có số cột tối đa

  • Số lượng ký tự trong dòng ít hơn hoặc bằng X (trong đó X là một số cố định các cột mỗi line_)
  • OR, có một dòng mới trong chuỗi gốc (sẽ buộc một "dòng" mới được tạo ra.

Tôi biết tôi có thể làm điều này theo thuật toán nhưng tôi tự hỏi nếu python có điều gì đó có thể xử lý trường hợp này. từ gói một chuỗi.

Và, bằng cách này, outpu t dòng phải được chia nhỏ trên ranh giới từ, không phải ranh giới ký tự.

Dưới đây là một ví dụ về đầu vào và đầu ra:

Input:

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

Output:

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

Trả lời

12

EDIT

gì bạn đang tìm kiếm là textwrap, nhưng đó là chỉ là một phần của giải pháp không hoàn thành một. Để đưa dòng mới vào tài khoản, bạn cần thực hiện việc này:

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

Câu trả lời hay! Đó là cách tôi nghĩ để làm điều đó, quá. Nhưng những gì với "Wilson, a" sau dòng trống? –

+0

@Andrei, tôi đã cập nhật câu trả lời của mình với giải pháp tốt hơn. –

+0

Tuyệt! Nhưng bây giờ có vẻ lộn xộn. : P –

Các vấn đề liên quan