简体   繁体   English

使用Python在txt文件中查找字符串

[英]Find String in a txt file using Python

I have a .txt file that is rather large--roughly 70K lines. 我有一个相当大的.txt文件-大约70K行。

I'm trying to use Python to find all the instances of the string "Cannot update". 我正在尝试使用Python查找字符串“无法更新”的所有实例。

When I open the file and use ctrl-f on "Cannot update" it instantly finds it; 当我打开文件并在“无法更新”上使用ctrl -f时,它会立即找到它。 however, when using RegEx in Python, .find(), or if in, it simply cannot find the string. 但是,在Python,.find()或inreg中使用RegEx时,它根本找不到该字符串。 Please see three methods I have used below: 请在下面查看我使用的三种方法:

RegEx method: RegEx方法:

f = open('C:\PerfupD.txt', 'r')

strings = re.findall('Cannot update', f.read())

print(strings)

.find(): 。找():

with open('C:\PerfUpD.txt', 'r') as file:

    for line in file:

          if line.find('Cannot update') != -1:

              print("Errors found")

if in: 如果在:

with open('C:\PerfUpD.txt', 'r') as file:

    for line in file:

          if 'Cannot update' in line:

              print("Errors found")

I even tried searching for "Ca" and it can't find anything, but when I just search "C" it finds tons of instances...One side note is that this .txt file is generated from a website that initially saves the file as a .err file. 我什至尝试搜索“ Ca”,但找不到任何内容,但是当我仅搜索“ C”时,它会找到大量实例...一个侧面说明是,该.txt文件是从最初保存该文件的网站生成的文件作为.err文件。 I then save it as .txt. 然后,我将其另存为.txt。

The only thing I can think of is that perhaps the data in the file is generated in some other form but looks like regular text when open. 我唯一想到的是,也许文件中的数据是以其他形式生成的,但是打开时看起来像常规文本。 Any insight is much appreciated! 非常感谢任何见解!

you could probably just do this: 您可能可以这样做:

f = open('your file.txt', 'r+')
for line in f:
    if 'Cannot Update' in line:
        print('error found')

no need for regex 无需正则表达式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM