问题描述
我正在使用Python从Web服务中收集数据.数据本身就是将呈现给用户的列表.我已经将它打印出来,就像这样:
I'm using Python to gather data from a Web service. The data itself is a list that will presented to users. I've managed it that it's printed out like:
( 1) Example of strings
( 2) Example 4
( 3) Another Example
( 4) Another Example 2
我正在对数字使用正则(2).但是,如果线条很长,打印效果会很像.断开宽度是终端的长度(rxvt,gnome-terminal):
I'm using rjust (2) for the numbers. However, if the lines are very long, the printing will be like. The breaking width is the length of terminal (rxvt, gnome-terminal):
( 1) Example of a very very very very long string and
that doesn't look fine
( 2) Example of indented long line that is very very
long, example line
( 3) Another Example
( 4) Another Example of a very very long string and
whatever here is
但是我想要的是这样的打印输出:
But what I've want is a print out like:
( 1) Example of a very very very very long string and
that doesn't look fine
( 2) Example of indented long line that is very very
long, example line
( 3) Another Example
( 4) Another Example of a very very long string and
whatever here is
有什么想法如何在不手动拆分行的情况下打印上述行?
Is there any ideas how to print the lines like above without breaking up the lines manually?
推荐答案
使用textwrap模块:http://docs.python.org/library/textwrap.html
Use the textwrap module: http://docs.python.org/library/textwrap.html
textwrap.fill(text, initial_indent='', subsequent_indent=' ')
这篇关于使用print()时如何换行和缩进长行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!