regex - Find String in a txt file using Python -
i have .txt file rather large--roughly 70k lines.
i'm trying use python find instances of string "cannot update".
when open file , use ctrl-f on "cannot update" instantly finds it; however, when using regex in python, .find(), or if in, cannot find string. please see 3 methods have used below:
regex method:
f = open('c:\perfupd.txt', 'r') strings = re.findall('cannot update', f.read()) print(strings) .find():
with open('c:\perfupd.txt', 'r') file: line in file: if line.find('cannot update') != -1: print("errors found") if in:
with open('c:\perfupd.txt', 'r') file: line in file: if 'cannot update' in line: print("errors found") i tried searching "ca" , can't find anything, when search "c" finds tons of instances...one side note .txt file generated website saves file .err file. save .txt.
the thing can think of perhaps data in file generated in other form looks regular text when open. insight appreciated!
you this:
f = open('your file.txt', 'r+') line in f: if 'cannot update' in line: print('error found') no need regex
Comments
Post a Comment