Monday, 19 August 2013

Read and search text file in Python?

Read and search text file in Python?

I need to write a Python script that reads a flat text file, searches for
a string, and rewrites into a new text file.
I have this (example) script:
import re
read = open("file.txt", "r")
data = file.read()
print data
newfile = open("newfile.txt", "w")
for line in read:
if re.match("(.*)dst="+str_var"(.*)", line):
print newfile, line,
file.close()
Is there an easier or more correct way? (I know pretty much nothing about
Python. This code is derived from what I've found in tutorials, google,
etc.)
Thanks!

No comments:

Post a Comment