本文介绍了使用print()时如何换行和缩进长行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我正在使用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()时如何换行和缩进长行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 21:18