搜索 .txt 中的所有字符串
Search if all string in .txt
我正在尝试 运行 一个程序来查看 .txt 文件,否则取决于内容
我以为会
Searhterms = [A, B]
with('output.txt') as f:
if ('A' and 'B') in f.read():
print('mix')
eLif ('A') in f.read:
if ('B') not in f.read:
print('ONLY A')
elif ('B') in f.read():
if ('A') not in f.read:
print('ONLY B')
else:
if ('A' and 'B') not in f.read:
print('NO AB)
但是,如果 A 和 B 存在,它会起作用,但如果只有一个,它会跳到另一个。越看越糊涂
如评论中所述,f.read()
一次性读取文件的所有内容,因此后续调用 f.read()
return 没有数据。您需要将数据存储在变量中。另外,您误解了 and
运算符的工作原理。
with open('output.txt', 'r') as f:
data = f.read()
if 'A' in data and 'B' in data:
print('mix')
elif 'A' in data:
print('ONLY A')
elif 'B' in data:
print('ONLY B')
else:
print('NO AB)
你最好用这个:
Searhterms = [A, B] # not sure why you need this
with('output.txt') as fin : # nice name for the INPUT file, btw
lines = fin.readlines()
for line in lines :
if ('A' in line) and ('B' in line):
print('mix')
eLif 'A' in line: # nice uppercase 'L', will puzzle the python
#if 'B' not in line: # don't need this
print('ONLY A')
elif 'B' in line:
#if 'A' not in line: # don't need this
print('ONLY B')
else:
#if ('A' and 'B') not in f.read: # this condition is not required
print('NO AB')
if len(lines) == 0 :
print('empty file')
我同意@Zabir Al Nazi 的观点。
读取 f.read() 清空文件
使用这个
Searhterms = ['A', 'B']
with open('output.txt') as f:
content = f.read()
if ('A' and 'B') in content:
print('mix')
elif 'A' in content:
if 'B' not in content:
print('ONLY A')
elif 'B' in content:
if 'A' not in content:
print('ONLY B')
else:
if ('A' and 'B') not in content:
print('NO AB')
您的代码有点问题。
首先,您应该只调用 f.read()
一次。
而且你的 if 语句构造错误。
尝试并分析这段代码:
with open('output.txt') as f:
lines = f.read()
if 'A' in lines and 'B' in lines:
print('mix')
elif 'A' in lines:
print('ONLY A')
elif 'B' in lines:
print('ONLY B')
else:
print('NO AB')
我正在尝试 运行 一个程序来查看 .txt 文件,否则取决于内容
我以为会
Searhterms = [A, B]
with('output.txt') as f:
if ('A' and 'B') in f.read():
print('mix')
eLif ('A') in f.read:
if ('B') not in f.read:
print('ONLY A')
elif ('B') in f.read():
if ('A') not in f.read:
print('ONLY B')
else:
if ('A' and 'B') not in f.read:
print('NO AB)
但是,如果 A 和 B 存在,它会起作用,但如果只有一个,它会跳到另一个。越看越糊涂
如评论中所述,f.read()
一次性读取文件的所有内容,因此后续调用 f.read()
return 没有数据。您需要将数据存储在变量中。另外,您误解了 and
运算符的工作原理。
with open('output.txt', 'r') as f:
data = f.read()
if 'A' in data and 'B' in data:
print('mix')
elif 'A' in data:
print('ONLY A')
elif 'B' in data:
print('ONLY B')
else:
print('NO AB)
你最好用这个:
Searhterms = [A, B] # not sure why you need this
with('output.txt') as fin : # nice name for the INPUT file, btw
lines = fin.readlines()
for line in lines :
if ('A' in line) and ('B' in line):
print('mix')
eLif 'A' in line: # nice uppercase 'L', will puzzle the python
#if 'B' not in line: # don't need this
print('ONLY A')
elif 'B' in line:
#if 'A' not in line: # don't need this
print('ONLY B')
else:
#if ('A' and 'B') not in f.read: # this condition is not required
print('NO AB')
if len(lines) == 0 :
print('empty file')
我同意@Zabir Al Nazi 的观点。 读取 f.read() 清空文件
使用这个
Searhterms = ['A', 'B']
with open('output.txt') as f:
content = f.read()
if ('A' and 'B') in content:
print('mix')
elif 'A' in content:
if 'B' not in content:
print('ONLY A')
elif 'B' in content:
if 'A' not in content:
print('ONLY B')
else:
if ('A' and 'B') not in content:
print('NO AB')
您的代码有点问题。
首先,您应该只调用 f.read()
一次。
而且你的 if 语句构造错误。
尝试并分析这段代码:
with open('output.txt') as f:
lines = f.read()
if 'A' in lines and 'B' in lines:
print('mix')
elif 'A' in lines:
print('ONLY A')
elif 'B' in lines:
print('ONLY B')
else:
print('NO AB')