【发布时间】:2014-01-20 18:02:54
【问题描述】:
我正在尝试将 .txt 列表中的每个单词连接到字符串 eg{ 'word' + str(1) = 'word1' }
使用此代码:
def function(item):
for i in range(2):
print item + str(i)
with open('dump/test.txt') as f:
for line in f:
function(str(line))
我将只使用仅包含两个单词('this'、'that')的 txt 文件。 我得到的是:
this
0
this
1
that0
that1
我所期待的:
this0
this1
that0
that1
如果我只使用function('this') 和function('that'),它可以正常工作,但为什么它不适用于 txt 输入?
--- 编辑: 解决了,谢谢! 问题是由
引起的收到的字符串中的换行符
解决方案:查看答案
【问题讨论】:
标签: python concatenation string-concatenation