2012-10-15 21 views
2

問題所以我剛剛重新格式化了一堆代碼,以合併textwrap.wrap,只發現我的所有\ n都不見了。這是一個例子。Python textwrap.wrap導致與 n

from textwrap import wrap 

def wrapAndPrint (msg, width=25): 
    """ wrap msg to width, then print """ 

    message = wrap(msg, width) 
    for line in message: 
    print line 

msg = ("Ok, this is a test. Let's write to a \nnewline\nand then " 
     "continue on with this message") 

如果我打印味精,我得到以下

>>> print msg 
Ok, this is a test. Let's write to a 
newline 
and then continue on with this message 
>>> 

但是,如果我把它被包裹它看起來像這樣:

>>> wrapAndPrint(msg) 
Ok, this is a test. Let's 
write to a newline and 
then continue on with 
this message 
>>> 

我認爲它可能有一些從列表中打印字符串,所以我試着調用特定的索引,所以我試着讓我的for循環看起來更像這樣:

x = 0 
for line in message: 
    print line[x] 
    x += 1 

但這並沒有任何區別。那麼,我在這裏錯過了什麼,或者做錯了什麼?我真的需要我的換行符,而且我還真的需要包裝我的文本。

回答