在 Python 中包装多行字符串(保留现有的换行符)?
Wrap multiline string (preserving existing linebreaks) in Python?
考虑这个例子:
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
首先 "joined" 多行字符串(即删除其中现有的换行符),然后才将其换行(即按给定数字拆分它个字符)。
如何换行多行字符串以保留换行符?也就是说,在这种情况下,预期输出将是:
['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 ??']
编辑;感谢@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" 个字符;所以这里的第一个元素本身就是一个多行字符串——所以它不是我所要求的,每一行都是一个数组元素。
拆分后把换行符加回来:
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 ??']
好的,我想我找到了做我想做的事的方法,但有点不雅:
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 ??']
所以:
- 第一个元素:'First line.' 最初定义
- 第二个元素:'Second line.' 也是最初定义的
- 第三个元素:原来的第三行太长,所以换行到80个字符,第一部分是数组中的第三个元素
- 第 4 个元素:包含原始第三行换行的第二部分
这就是我想要实现的;请注意,这与数组中第一个元素包含多行的情况非常不同,如 ['\nFirst line.\nSecond line.\nThe third line ...
.
考虑这个例子:
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
首先 "joined" 多行字符串(即删除其中现有的换行符),然后才将其换行(即按给定数字拆分它个字符)。
如何换行多行字符串以保留换行符?也就是说,在这种情况下,预期输出将是:
['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 ??']
编辑;感谢@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" 个字符;所以这里的第一个元素本身就是一个多行字符串——所以它不是我所要求的,每一行都是一个数组元素。
拆分后把换行符加回来:
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 ??']
好的,我想我找到了做我想做的事的方法,但有点不雅:
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 ??']
所以:
- 第一个元素:'First line.' 最初定义
- 第二个元素:'Second line.' 也是最初定义的
- 第三个元素:原来的第三行太长,所以换行到80个字符,第一部分是数组中的第三个元素
- 第 4 个元素:包含原始第三行换行的第二部分
这就是我想要实现的;请注意,这与数组中第一个元素包含多行的情况非常不同,如 ['\nFirst line.\nSecond line.\nThe third line ...
.