jts*_*287 2 python newline word-wrap
所以我只是重新格式化了一堆代码以合并textwrap.wrap,但却发现我的所有内容都已消失.这是一个例子.
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")
Run Code Online (Sandbox Code Playgroud)
如果我打印msg,我会得到以下内容
>>> print msg
Ok, this is a test. Let's write to a
newline
and then continue on with this message
>>>
Run Code Online (Sandbox Code Playgroud)
但是,如果我发送它包装它看起来像这样:
>>> wrapAndPrint(msg)
Ok, this is a test. Let's
write to a newline and
then continue on with
this message
>>>
Run Code Online (Sandbox Code Playgroud)
我认为它可能与从列表中打印字符串有关,所以我尝试调用特定索引,所以我尝试使我的for循环看起来更像这样:
x = 0
for line in message:
print line[x]
x += 1
Run Code Online (Sandbox Code Playgroud)
但这没有任何区别.那么,我在这里错过了什么,或者做错了什么?我真的需要我的新行,我也真的需要我的文字被包装.
默认情况下,该wrap()函数用单个空格替换空白字符(包括换行符).您可以通过传递关键字参数来关闭它replace_whitespace=False.
http://docs.python.org/library/textwrap.html#textwrap.TextWrapper.replace_whitespace
| 归档时间: |
|
| 查看次数: |
2454 次 |
| 最近记录: |