Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python:TypeError:Can';t转换为';列表';对象隐式地访问str_Python_Search_Typeerror - Fatal编程技术网

python:TypeError:Can';t转换为';列表';对象隐式地访问str

python:TypeError:Can';t转换为';列表';对象隐式地访问str,python,search,typeerror,Python,Search,Typeerror,我在使用Python3.1.4时遇到了以下错误,它过去在Python2.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! 在 更新1: 我正在尝试从文件中的关键字创建一个列表。每行有一个关键字。我是否正确读取了文件 keyword_file=r"KE

我在使用Python3.1.4时遇到了以下错误,它过去在Python2.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!

更新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']

的列表,这意味着您的关键字对象包含列表

# this is valid:
import re
keywords=["a","b","c"]

for word in keywords: # Iterate through keywords
    if re.search(r"\b"+word+r"\b",line1):
        print "ok"

# this is not valid. This is the kind of error you get:    
keywords=[["a","b"],"c"]

for word in keywords: # Iterate through keywords
    if re.search(r"\b"+word+r"\b",line1):
        print "ok"

您应该打印
word
,以确保您理解它是什么。您可能希望使用
”。在正则表达式中使用join(word)
而不是
word

虽然行已被
readlines()
拆分,但仍拆分了行。这应该起作用:

# actually no need for readline() here, the file object can be
# directly used to iterate over the lines
keywords = (line.strip().lower() for line in f0)
# ...
for word in keywords:
  if re.search(r"\b"+word+r"\b",line1):
这里使用的是生成器表达式。您应该了解这些,它们非常方便,并且可以用来替换
map
filter

请注意,在循环之前创建正则表达式可能更有效,如下所示:

keywords = (line.strip() for line in f0)
# use re.escape here in case the keyword contains a special regex character
regex = r'\b({0})\b'.format('|'.join(map(re.escape, keywords)))
# pre-compile the regex (build up the state machine)
regex = re.compile(regex, re.IGNORECASE)

# inside the loop over the lines
if regex.search(line1)
  print "ok"

无论是
word
还是
line1
似乎都是列表而不是字符串,正如您所期望的那样。哪一个我说不出来,你必须提供更多的代码。这里的列表是什么?第一行?re.search()接受要搜索的模式和字符串,而不是列表。@NiklasB。你说得对。单词是一个列表。有关更多信息,请参阅我的编辑问题。我无法以正确的格式导入列表。@Pradeep:我看不到任何更新。谢谢Niklas。关键字列表不是expect格式。我无法查看它,因为它是生成器对象。通过使用列表理解,是否可以让它看起来像关键字=['Keyword1','Keyword2',…,'Keywordn']是的:
关键字=[x.strip().lower(),在f0.readlines()]中代表x
拖放
.readlines()
。你不需要在这里创建一个行列表。如果
关键字
可能重叠,那么较长的关键字应该在较短的关键字之前(如果你想提取匹配项)。@J.F.Sebastian:第一个建议你说得对,我编辑了这个(复制原始代码时一定错过了)。不过,我不太明白你的第二点:为什么要选择更长的关键词?我们还使用
\b
来匹配单词边界。
keywords = (line.strip() for line in f0)
# use re.escape here in case the keyword contains a special regex character
regex = r'\b({0})\b'.format('|'.join(map(re.escape, keywords)))
# pre-compile the regex (build up the state machine)
regex = re.compile(regex, re.IGNORECASE)

# inside the loop over the lines
if regex.search(line1)
  print "ok"