如何在文本文件中搜索字符串? [英] How to search for a string in text files?
问题描述
我想检查一个字符串是否在文本文件中.如果是,则执行 X.如果不是,则执行 Y.但是,由于某种原因,此代码总是返回 True
.谁能看出哪里出了问题?
I want to check if a string is in a text file. If it is, do X. If it's not, do Y. However, this code always returns True
for some reason. Can anyone see what is wrong?
def check():
datafile = file('example.txt')
found = False
for line in datafile:
if blabla in line:
found = True
break
check()
if True:
print "true"
else:
print "false"
推荐答案
你总是得到 True
的原因已经给出,所以我再提供一个建议:
The reason why you always got True
has already been given, so I'll just offer another suggestion:
如果您的文件不是太大,您可以将其读入一个字符串,然后直接使用它(比每行读取和检查行更容易且通常更快):
If your file is not too large, you can read it into a string, and just use that (easier and often faster than reading and checking line per line):
with open('example.txt') as f:
if 'blabla' in f.read():
print("true")
另一个技巧:您可以通过使用 mmap.mmap() 来缓解可能出现的内存问题
创建一个使用底层文件的类字符串"对象(而不是在内存中读取整个文件):
Another trick: you can alleviate the possible memory problems by using mmap.mmap()
to create a "string-like" object that uses the underlying file (instead of reading the whole file in memory):
import mmap
with open('example.txt') as f:
s = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)
if s.find('blabla') != -1:
print('true')
注意:在 python 3 中,mmap 的行为类似于 bytearray
对象而不是字符串,因此您使用 find()
查找的子序列必须是 bytes
对象而不是字符串,例如.s.find(b'blabla')
:
NOTE: in python 3, mmaps behave like bytearray
objects rather than strings, so the subsequence you look for with find()
has to be a bytes
object rather than a string as well, eg. s.find(b'blabla')
:
#!/usr/bin/env python3
import mmap
with open('example.txt', 'rb', 0) as file, \
mmap.mmap(file.fileno(), 0, access=mmap.ACCESS_READ) as s:
if s.find(b'blabla') != -1:
print('true')
您也可以在 mmap
上使用正则表达式,例如不区分大小写的搜索:if re.search(br'(?i)blabla', s):
You could also use regular expressions on mmap
e.g., case-insensitive search: if re.search(br'(?i)blabla', s):
这篇关于如何在文本文件中搜索字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!