I am getting the following error with Python 3.1.4 which used to work well in 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!
In
for word in keywords: # Iterate through keywords
if re.search(r"\b"+word+r"\b",line1): #Search kewords in the input line
Update1:
I am trying to create a list from keywords which is in a file. Each line has one keyword. Am I reading the file properly?
keyword_file=r"KEYWORDS.txt"
f0=open(keyword_file,'r')
keywords = map(lambda a: a.split('\n'),map(str.lower, f0.readlines()))
keyword file contains:
Keyword1
Keyword2
.
.
.
Keywordn
I want a list called keywords = ['Keyword1','Keyword2',...,'Keywordn']
You split the lines although they have already been split by
readlines(). This should work:What’s used here is a generator expression. You should learn about those, they are quite handy, as well as about list comprehensions which can often be used to replace
mapandfilter.Note that it might be more performant to create the regular expression before the loop, like this: