6

我有一个长字符串(多个段落),我需要将其拆分为行字符串列表。确定什么构成“线”基于:

  • 行中的字符数小于或等于 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."
4

2 回答 2

14

编辑

您正在寻找的是textwrap,但这只是解决方案的一部分,而不是完整的解决方案。要考虑换行,您需要执行以下操作:

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
于 2009-09-10T17:05:17.237 回答
4

您可能想使用标准库中的 textwrap 函数:

http://docs.python.org/library/textwrap.html

于 2009-09-10T17:03:29.760 回答