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

Popular posts from this blog

Date format issue with spark sql

Hive Partition sub folders HIVE_UNION_SUBDIR_1,HIVE_UNION_SUBDIR_2,HIVE_UNION_SUBDIR_8

Dataiku and Dremio date difference