I'm trying to convert a file from csv to xml by doing some replaces. I don't know if this is the best way but sounded a simpler and easy way to do it, since the files are quite simple. The thing is, the last replace I have to do is a regex and I don't know how to append that to all the replaces that come before.
import re
text = open("input.csv", "r")
x = open("output.xml","w")
for line in text:
x.write(line.replace("QWERT=", "<Doc3045 QWERT=\"") \
.replace(",Date=", "\" Date=\"") \
.replace("ABCDE,Tp,CD", "") \
.replace("S,1,", "<Cli Tp=\"1\" ABCDE=\"S\" Cd=\"") \
.replace(",", "\">"))
#regex replace should go here
text.close()
x.close()
I already have the regex replace ready:
line = re.sub(r'(\d$)', r'\1"/>', line)
but I couldn't find a way to apply it inside the for
within the input file or even to open the output file and applying the regex.