考虑这个例子:
import textwrap
import pprint
mystr=r"""
First line.
Second line.
The third line is a very long line, which I would like to somehow wrap; wrap at 80 characters - or less, or more! ... can it really be done ??"""
pprint.pprint(textwrap.wrap(mystr,80))
字符串mystr
已经是一个多行字符串,因为它包含换行符;但是,如果我运行这个脚本,我得到输出:
[' First line. Second line. The third line is a very long line, which I would like',
'to somehow wrap; wrap at 80 characters - or less, or more! ... can it really be',
'done ??']
...这意味着textwrap.wrap
首先加入"多行字符串(即删除其中的现有换行符),然后将其包装(即将其拆分为给定的字符数)。
如何换行多行字符串,以便保留换行符?也就是说,在这种情况下,预期的输出将是:
['First line.',
'Second line.',
'The third line is a very long line, which I would like to somehow wrap; wrap at',
'80 characters - or less, or more! ... can it really be done ??']
EDIT;感谢@u_mulder的评论,我试过了:
textwrap.wrap(mystr,80,replace_whitespace=False)
然后我得到:
['\nFirst line.\nSecond line.\nThe third line is a very long line, which I would like',
'to somehow wrap; wrap at 80 characters - or less, or more! ... can it really be',
'done ??']
换行符似乎被保留了,但是" inline"字符;所以这里第一个元素本身就是一个多行字符串 - 所以它不是我需要它,每一行都是一个数组元素。
答案 0 :(得分:2)
分割后只需添加新行:
import textwrap
import pprint
import itertools
mystr=r"""
First line.
Second line.
The third line is a very long line, which I would like to somehow wrap; wrap at 80 characters - or less, or more! ... can it really be done ??"""
wrapper = textwrap.TextWrapper(width = 80)
mylist = [wrapper.wrap(i) for i in mystr.split('\n') if i != '']
mylist = list(itertools.chain.from_iterable(mylist))
pprint.pprint(mylist)
输出:
['First line.',
'Second line.',
'The third line is a very long line, which I would like to somehow wrap; wrap at',
'80 characters - or less, or more! ... can it really be done ??']
答案 1 :(得分:2)
好吧,我想我找到了如何做我想做的事,但它有点不优雅:
import textwrap
import pprint
mystr=r"""
First line.
Second line.
The third line is a very long line, which I would like to somehow wrap; wrap at 80 characters - or less, or more! ... can it really be done ??"""
#pprint.pprint(textwrap.wrap(mystr,80,replace_whitespace=False))
aa=[]
for ix in mystr.splitlines():
if ix:
if len(ix)<=80: aa.append(ix)
else: aa.extend(textwrap.wrap(ix,80))
pprint.pprint(aa)
结果如下:
['First line.',
'Second line.',
'The third line is a very long line, which I would like to somehow wrap; wrap at',
'80 characters - or less, or more! ... can it really be done ??']
所以:
这就是我想要实现的目标;请注意,这与数组中的第一个元素包含多行时的情况完全不同,如['\nFirst line.\nSecond line.\nThe third line ...
中所示。