Python 使用文件在定义的列表中查找短语

Python 使用文件在定义的列表中查找短语,python,Python,我有一个列表名my=[“cbs停机”,“异常”] 我在读取模式下打开了一个文件 现在,我想搜索该文件中列表中存在的任何可用字符串,并执行if操作 fopen = open("test.txt","r") my =['cbs is down', 'abnormal'] for line in fopen: if my in line: print ("down") 当我执行它时,我会得到以下结果 Traceback (most recent call last

我有一个列表名
my=[“cbs停机”,“异常”]
我在读取模式下打开了一个文件

现在,我想搜索该文件中列表中存在的任何可用字符串,并执行if操作

fopen  =  open("test.txt","r")
my =['cbs is down', 'abnormal']
for line in fopen:
    if my in line:
            print ("down")
当我执行它时,我会得到以下结果

Traceback (most recent call last):
  File "E:/python/fileread.py", line 4, in <module>
    if my in line:
TypeError: 'in <string>' requires string as left operand, not list
回溯(最近一次呼叫最后一次):
文件“E:/python/fileread.py”,第4行,在
如果我的产品符合要求:
TypeError:“in”需要字符串作为左操作数,而不是列表
样本输入

Some text cbs is down
Yes, abnormal
not in my list
cbs is down
输出

down
down
down
样本输入

Some text cbs is down
Yes, abnormal
not in my list
cbs is down
输出

down
down
down

这应该会解决问题:

if any(i in line for i in my):
    ...

基本上,您正在查看my并检查它的任何元素是否在行中。

这应该会解决问题:

if any(i in line for i in my):
    ...

基本上,您正在检查
my
并检查其元素是否有任何

您也可以
my
中的术语连接到一个类似的
\b(cbs已关闭|异常)\b
并使用
re.findall
re.search
查找术语。通过这种方式,您还可以将模式括在单词边界
\b..\b
中,使其与较长单词的部分不匹配,并且您还可以查看匹配的术语和位置

>>> import re
>>> my = ['cbs is down', 'abnormal']
>>> line = "notacbs is downright abnormal"
>>> p = re.compile(r"\b(" + "|".join(map(re.escape, my)) + r")\b")
>>> p.findall(line)
['abnormal']
>>> p.search(line).span()
(21, 29)

您还可以
my
中的术语加入类似的
\b(cbs关闭|异常)\b
并使用
re.findall
re.search
查找术语。通过这种方式,您还可以将模式括在单词边界
\b..\b
中,使其与较长单词的部分不匹配,并且您还可以查看匹配的术语和位置

>>> import re
>>> my = ['cbs is down', 'abnormal']
>>> line = "notacbs is downright abnormal"
>>> p = re.compile(r"\b(" + "|".join(map(re.escape, my)) + r")\b")
>>> p.findall(line)
['abnormal']
>>> p.search(line).span()
(21, 29)
错误原因:

中使用的
运算符:

if my in line: ...
   ^       ^
   |_ left | hand side 
           |
           |_ right hand side
对于右侧的字符串操作数(即
),左侧需要相应的字符串操作数。此操作数一致性检查由
str.\uuuuuu contains\uuuuu
方法实现,其中对
\uuuuuuuu contains\uuuuu
的调用来自右侧的字符串(请参阅)。同:

if line.__contains__(my): ...
但是,您传递的是一个列表,
my
,而不是一个字符串

解决此问题的简单方法是使用内置函数检查列表中的任何项目是否包含在当前行中:

for line in fopen:
    if any(item in line for item in my):
       ...
或者,由于您只有两个项目,请使用与
any
相同的短路方式的运算符(双关语,意料之外):

for line in fopen:
    if 'cbs is down' in line or 'abnormal' in line:
       ...
错误原因:

中使用的
运算符:

if my in line: ...
   ^       ^
   |_ left | hand side 
           |
           |_ right hand side
对于右侧的字符串操作数(即
),左侧需要相应的字符串操作数。此操作数一致性检查由
str.\uuuuuu contains\uuuuu
方法实现,其中对
\uuuuuuuu contains\uuuuu
的调用来自右侧的字符串(请参阅)。同:

if line.__contains__(my): ...
但是,您传递的是一个列表,
my
,而不是一个字符串

解决此问题的简单方法是使用内置函数检查列表中的任何项目是否包含在当前行中:

for line in fopen:
    if any(item in line for item in my):
       ...
或者,由于您只有两个项目,请使用与
any
相同的短路方式的运算符(双关语,意料之外):

for line in fopen:
    if 'cbs is down' in line or 'abnormal' in line:
       ...

对于
my
中的每个匹配项,这会向下打印一次
。也许不是OP想要的,但我想不是。另一方面,您知道找到了哪个
x
,并且在第一个之后仍然可以
break
。这会为
my
中的每个匹配项向下打印
一次。也许不是OP想要的,但我想不是。另一方面,您知道找到了哪个
x
,并且在第一个
之后仍然可以
中断