Convert csv to XML file
Please find below code to convert csv file to xml.
# -*- coding: utf-8 -*-
import dataiku
import pandas as pd, numpy as np
from dataiku import pandasutils as pdu
from lxml import etree as et
Dept_3 = dataiku.Dataset("Dept_3")
df = Dept_3.get_dataframe()
df['deptno'] = df['deptno'].astype(str)
root= et.Element('data')
# iterate over rows and add to the tree
for ix, row in df.iterrows():
item = et.SubElement(root, 'item', attrib=row.to_dict());
# get a handle on the folder to write
xml_files = dataiku.Folder("xfile")
with xml_files.get_writer("dept.xml") as w:
w.write(et.tostring(root, encoding='UTF-8', xml_declaration=False))
Comments
Post a Comment