1

The code below reads an xml file, it then runs a function and as the last step it parses the data into an xlsx file.

if __name__ == '__main__':
    xml = XMLParser.parse(r'./work.xml')
    xml.recurs_collect(opdata_collector)
    df.to_excel("./output.xlsx")

If I want to include a second xml files, I have to basically copy and paste the code and write the name of the new xml file.

if __name__ == '__main__':
        xml = XMLParser.parse(r'./work.xml')
        xml.recurs_collect(opdata_collector)
        xml = XMLParser.parse(r'./work2.xml')
        xml.recurs_collect(opdata_collector)
        df.to_excel("./output.xlsx")

I have tried the following but without success

directory = 'C:\Users\342\Desktop\num\docs\test'
for filename in os.listdir(directory):
    if filename.endswith(".xml"):
        xml.recurs_collect(opdata_collector)
        df.to_excel("./output.xlsx")
        continue
    else:
        continue

1 Answers1

1

When you say

directory = 'C:\Users\342\Desktop\num\docs\test'

you are directing python to escape U, 3, D, etc. This should result in a syntax error (SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape). There are many ways to handle Windows paths in python, but your best bet is going to be pathlib.

I'm not on a windows machine to test this, but I think something like the following should work:

directory = PureWindowsPath('C:/Users/342/Desktop/num/docs/test')
...
thisisrandy
  • 2,660
  • 2
  • 12
  • 25