I would like to parse the data with 'tab delimited' and would like to replace certain string in the data.
input file: vi foo.txt:
Bob lives in%3a Boston
Sam lives in Houston
Jay lives in Ruston
Bill lives in Atlanta
This is what I came up with: vi foo.py:
import re
fin = open("foo.txt")
fout = open("bar.txt", "w")
for line in fin.readlines():
fout.write('\t'.join(line.split())+'\n') # parse data with tab delimited
for line in fin.readlines():
fout.write(re.sub('%3a',':',line)) # substitute string with regex
vi bar.txt:
Bob lives in%3a Boston
Sam lives in Houston
Jay lives in Ruston
Bill lives in Atlanta
Why is %3a still in output rather than ':'?
Thanks,
Rio
fin.seek(0), then you will see 8 lines instead of 4 - the first set would have%3a, and second would have a: