2010-12-04 60 views
4

我正在使用Python从Web服务中收集数据。数据本身就是一个将呈现给用户的列表。我已经把它打印出来了,如下所示:如何在Python中使用print()时打包和缩进长行?

(1) Example of strings 
(2) Example 4 
(3) Another Example 
(4) Another Example 2 

我对数字使用rjust(2)。但是,如果线条很长,打印就会如此。断裂宽度为终端的长度(rxvt的,GNOME终端):

(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 

但是我要的是一个打印出像:

(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 

是否有任何想法如何打印像上面的线条没有手动分割线条?

回答

8
+2

谢谢!正是我需要的。我已经使用了这个方法:textwrap.fill(text,initial_indent ='',subsequent_indent ='') – 2010-12-04 18:59:15

相关问题