我有一个长字符串(多个段落),我需要将其拆分为行字符串列表。确定什么构成“线”基于:
- 行中的字符数小于或等于 X(其中 X 是每行的固定列数_)
- 或者,原始字符串中有一个换行符(这将强制创建一个新的“行”。
我知道我可以通过算法做到这一点,但我想知道 python 是否有可以处理这种情况的东西。它本质上是对字符串进行自动换行。
而且,顺便说一句,输出行必须在字边界上断开,而不是在字符边界上。
这是输入和输出的示例:
输入:
"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."
输出:
"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."