I have a sample text file having data like this
<iden/><provider></provider><trace>065110d4-cec5-d433772ed57a</trace>
<ServiceRQ>Some xml data</ServiceRQ>
<iden/><provider></provider>
<ServiceRQ>Some xml data</ServiceRQ>
like this and so on it is quite a large file.
I want to check the odd line if <trace>065110d4-cec5-43f9-b089-d433772ed57a</trace>
is present that replace it with <trace>xyz</trace>
else if <trace>065110d4-cec5-43f9-b089-d433772ed57a</trace>
is not present then add <trace>xyz</trace>
my code:
with open("Sample_xml.txt", 'r') as fp:
output = fp.readlines()
type(output)
s = len(output) - 1
tc = 0
rq = 1
while (tc <= s) and (rq <= s):
if tc % 2 == 0:
a = (output[tc])
if a.find("<trace") != -1:
a = re.sub('(?<=<trace>)(.*?)(?=</trace>)','xyz', a)
print(a)
elif a.find("<trace>") == -1:
a = a.rstrip() + '<trace>xyz</trace>' +'\n'
print(a)
if rq % 2 != 0:
b = (output[rq])
print(b)
with open("Fin_xml.txt", "a") as myfile:
myfile.write(a)
myfile.write(b + '\n')
tc += 2
rq += 2
the file is so large greater that 800mb so this code is not working properly with readlines(). Please can someone help me with my code