textwrap.fill在从Python类打印时提供奇怪的结果

textwrap.fill在从Python类打印时提供奇怪的结果,python,python-3.x,Python,Python 3.x,虽然textwrap.fill可用于手动输入的文本,即打印(“(真正的长字符串)”),但当从csv文件填充的类打印时,它会产生意外的结果 例如: print(item.name,":",(textwrap.fill(item.description)) 我得到以下信息: Hide Armor : This crude armor consists of thick furs and hides. It is commonly worn by barbarian tribes, evil h

虽然textwrap.fill可用于手动输入的文本,即打印(“(真正的长字符串)”),但当从csv文件填充的类打印时,它会产生意外的结果

例如:

print(item.name,":",(textwrap.fill(item.description)) 
我得到以下信息:

 Hide Armor : This crude armor consists of thick furs and hides. It is commonly
worn
by barbarian tribes, evil humanoids, and other folk who lack access to
the tools and materials needed to create better armor. 
有什么想法吗?CSV中已经没有换行符

CSV中的行显示:

Hide Armor,10,12 + Dex modifier (max 2),-,-,20 lb.,"This crude armor consists of thick furs and hides. It is commonly worn by barbarian tribes, evil humanoids, and other folk who lack access to the tools and materials needed to create better armor.",Medium
那是一行


关于删除随机换行有什么想法吗?是否可以在输出的开头使用随机空格?我不知道空白是从哪里来的,甚至在不使用textwrap模块时也会出现。

我怀疑您的终端或控制台正在进行包装;换行时需要考虑行中的前缀。将其包含在要包装的文本中:

print(textwrap.fill('{}: {}'.format(item.name, item.description)))

我在这里使用
.name
.description
属性创建了一个新字符串,中间有一个冒号。

看起来第一行比您的终端宽度长,您的终端(或windows控制台)正在进行包装。我目前正在geany中编译和测试。我无法改变那里的终端宽度进行测试,所以我将直接从Ubuntu终端开始尝试。