2010-12-04 21 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

相關問題