#!/usr/bin/python # Creating an output file in writing mode output_file = open("newfile.txt", "w") # write 3 header records output_file.write('<?xml version="1.0" encoding="utf-8"?>\n') output_file.write("<!DOCTYPE KMYMONEY-FILE>\n") output_file.write("<KMYMONEY-FILE>\n") write_flag = 0 # Open the file in read mode with open('Australian-2024-11-30.xml', 'r') as file: # Read each line in the file for line in file: string = line sub_str1 = "<TRANSACTIONS" sub_str2 = " <SCHEDULES count" if sub_str1 in string: print("YES") write_flag = 1 #commence writing to newfile.txt elif sub_str2 in string: write_flag = 0 #stop writing when this string found print("schedules found") if write_flag: output_file.write(file.read()) # Close the output file output_file.close()The output has all the "<TRANSACTIONS" tag and associated children, BUT it also has all the "<SCHEDULES" tag , plus all data after that. The variable "write_flag" is not being turned off, despite the fact that the "schedules" tag is present ?
In the data, there is only one occurence of "sub_str1" and "sub_str2". So the writes to the output get turned ON at sub_str1 and then turned OFF at sub_str2. But once that flag is on, it stays on, which suggests the
elif sub_str2 in string:is not being tested. Or is being tested, yet returns false.