Python:将textwrap转换为列表

0 投票
3 回答
826 浏览
提问于 2025-04-16 05:10

我有一个变量:

row='saint george 1739 1799 violin concerti g 029 039 050 symphonie concertante for two violins g 024 bertrand cervera in 024 039 christophe guiot in 024 029 and thibault vieux violin soloists orchestre les archets de paris'

我正在做这个:

textwrap.fill(row,55)

我希望有一个列表 line,让它的内容是 line[0]='saint george 1739 1799 violin concerti g 029 039 050'line[1]='symphonie concertante for two violins g 024 bertrand' 等等……

请帮我把这个文本转换成一个列表

请注意,textwrap 是通过 \n 来分隔内容的

3 个回答

1

你可以直接用split这个方法来分割它,比如说:

textwrap.fill(row, 55).split('\n')

或者你也可以用textwrap.wrap来代替。

1

textwrap.wrap 会返回一个列表。那为什么不直接用这个呢?

textwrap.fill(text, ...) 相当于 "\n".join(wrap(text, ...))。这个在文档中有解释。

1

textwrap.wrap 替代 textwrap.fill

撰写回答