I have the following code which creates a xml file from each row in excel. I want to write the xml files in a new folder (currently generates files in same folder as python), how do i change the "output.write()" statement? I am new in programming, started with python 2 weeks ago.
df_data is a list of dictionaries.
[{'Nume': 'Ciuica', 'Prenume': 'Larisa Bianca', 'Data': '30/01/1991 00:00:00', 'Cod': 789454, 'Localitate': 'Targu Jiu'},
{'Nume': 'Balasa', 'Prenume': 'Valentin', 'Data': '14/09/1989 00:00:00', 'Cod': 215487, 'Localitate': 'Bucuresti'},
{'Nume': 'Vinatoru', 'Prenume': 'Costin', 'Data': '06/12/1980 00:00:00', 'Cod': 254896, 'Localitate': 'Titu'}]
Code:
import xml.etree.ElementTree as Et
import pandas as pd
wb_data = pd.read_excel("test.xlsx", sheet_name="Data", header=0)
wb_xml = pd.read_excel("test.xlsx", sheet_name="XML", header=0)
df_data = wb_data.to_dict("records")
lista_xml = []
index_lista = 0
for xml in wb_xml["Denumire_xml"]:
lista_xml.append(xml)
for row in df_data:
document = Et.Element("Document")
for key, value in row.items():
element = Et.SubElement(document, key)
element.text = str(value)
output = Et.ElementTree(document)
Et.indent(output)
output.write(lista_xml[index_lista])
index_lista = index_lista 1
CodePudding user response:
Simply use absolute path especially with os.path.join
to concatenate folder and file name:
import os
...
xml_file_path = "/path/to/my/xml/outputs"
output.write(
os.path.join(xml_file_path, lista_xml[index_lista])
)
However, consider the new DataFrame.to_xml()
to export data frame rows to XML files:
import os
import pandas as pd
xml_file_path = "/path/to/my/xml/outputs"
wb_data = pd.read_excel("test.xlsx", sheet_name="Data", header=0)
wb_xml = pd.read_excel("test.xlsx", sheet_name="XML", header=0)
# LIST COMPREHENSION
lista_xml = [xml for xml in wb_xml["Denumire_xml"]]
# SERIES TO LIST CONVERSION
lista_xml = wb_xml["Denumire_xml"].tolist()
# GROUP BY INDEX (I.E., ROW) WITH LOOP NUMBER
for n, (i, sub_df) in enumerate(wb_data.groupby(level=0)):
sub_df.to_xml(
os.path.join(xml_file_path, lista_xml[n])
)