How to convert this csv format['x','y','width','height','tag']
to this XML format using python script?
<annotation>
<object>
<tag>figure</tag>
<bndbox>
<x>1.0</x>
<y>100.0</y>
<width>303.0</width>
<height>619.0</height>
</bndbox>
</object>
<object>
<tag>text</tag>
<bndbox>
<x>338.0</x>
<y>162.0</y>
<width>143.0</width>
<height>423.0</height>
</bndbox>
</object>
<object>
<tag>text</tag>
<bndbox>
<x>85.0</x>
<y>768.0</y>
<width>554.0</width>
<height>39.0</height>
</bndbox>
</object>
</annotation>
Note this is for the first row of the csv file i want to convert for all row
CodePudding user response:
You can use python Panda for this https://pypi.org/project/pandas/
imports pandas as pd
df= pd.read_csv('movies2.csv')
with open('outputf.xml', 'w') as myfile:
myfile.write(df.to_xml())
CodePudding user response:
make sure you have titles in your csv .
import pandas as pd
def convert_row(row):
return """<annotation>
<object>
<tag>%s</tag>
<bndbox>
<x>%s</x>
<y>%s</y>
<width>%s</width>
<height>%s</height>
</bndbox>
</object>""" % (
row.tag, row.x, row.y, row.width, row.hight)
df = pd.read_csv('untitled.txt', sep=',')
print '\n'.join(df.apply(convert_row, axis=1))