regex - Python open text file, do multiline regular expressions, write out file -
i have spent years in 90s doing regular expressions on perl have been out of since , python newbie. syntactical wrappers need job in python? @ now, result computer doing nothing , it.:
import os import re os.chdir("/users/.../") atext = open("textfile.txt", 'r').read() atext = re.sub(r'foo', r'bar', atext.rstrip()) print atext
your problem .read()
not read whole file, reads file given size in bytes: https://docs.python.org/2/tutorial/inputoutput.html#methods-of-file-objects
what need, read file line line, can use atext.readlines()
return whole file lines in list, or use code faster .readlines()
import os import re os.chdir("/users/.../") open("textfile.txt", 'r') atext: line in atext: line = re.sub(r'foo', r'bar', line.rstrip()) print line
Comments
Post a Comment