2012-10-15 16 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 

但这并没有任何区别。那么,我在这里错过了什么,或者做错了什么?我真的需要我的换行符,而且我还真的需要包装我的文本。

回答