Read and search text file in Python? -
i need write python script reads flat text file, searches string, , rewrites new text file.
i have (example) script:
import re read = open("file.txt", "r") data = file.read() print data newfile = open("newfile.txt", "w") line in read: if re.match("(.*)dst="+str_var"(.*)", line): print newfile, line, file.close()
is there easier or more correct way? (i know pretty nothing python. code derived i've found in tutorials, google, etc.)
thanks!
this may trick
read_file = open("file.txt", "r") data = read_file.read() read_file.close() file_content = re.sub("\d+", "", data) word = your_word if word in file_content: newfile = open("newfile.txt", "w") print >> newfile, word newfile.close()
Comments
Post a Comment