【发布时间】:2014-03-04 00:40:01
【问题描述】:
我在使用 Python 3.1.4 时遇到以下错误,它曾经在 Python 2.7.2 中运行良好。
TypeError: Can't convert 'list' object to str implicitly. I get the error on the if statement. Please let me know how to fix this. Thanks!
在
for word in keywords: # Iterate through keywords
if re.search(r"\b"+word+r"\b",line1): #Search kewords in the input line
更新1:
我正在尝试从文件中的关键字创建一个列表。每一行都有一个关键字。我是否正确读取文件?
keyword_file=r"KEYWORDS.txt"
f0=open(keyword_file,'r')
keywords = map(lambda a: a.split('\n'),map(str.lower, f0.readlines()))
关键字文件包含:
Keyword1
Keyword2
.
.
.
Keywordn
我想要一个名为keywords = ['Keyword1','Keyword2',...,'Keywordn']的列表
【问题讨论】:
-
word或line1似乎是列表而不是字符串,如您所料。哪个我不知道,你必须提供更多代码。 -
这里的列表是什么?第 1 行? re.search() 采用要搜索的模式和字符串,而不是列表。
-
@NiklasB。你是对的。 Word 是一个列表。请参阅我的编辑问题以获取更多信息。我无法以正确的格式导入列表。
-
@Pradeep:我没有看到任何更新..