I have a csv file which resembles the format below:
===============================================================
#Type 1 Definition
#============================================================================
#TYPE, <name>
#Some tag for type------------------------------------------------------
#TYPESomeTag, <id>, <name>, <param>
#Another tag for type----------------------------------------------
#TYPEAnothertag, <param_1>, <param_2>, <param_3>
TYPE, Name_1
TYPESomeTag, 1, 2, 3
TYPESomeTag, 4, 2, 5
TYPEAnothertag, a, b, c
TYPE, Name_2
TYPESomeTag, 1, 2, 3
TYPESomeTag, 4, 2, 5
TYPEAnothertag, a, b, c
#===============================================================================
#Type 2 Definition
#===============================================================================
#TYPE2, <name>
#Some tag for type------------------------------------------------------
#TYPE2SomeTag, <id>, <name>, <param>
#Another tag for type----------------------------------------------
#TYPE2Anothertag, <param_1>, <param_2>, <param_3>
TYPE2, Name_1
TYPE2SomeTag, 1, 2, 3
TYPE2SomeTag, 4, 2, 5
TYPE2Anothertag, a, b, c
TYPE2, Name_2
TYPE2SomeTag, 1, 2, 3
TYPE2SomeTag, 4, 2, 5
TYPE2Anothertag, a, b, c
and so on...
My goal is to convert the above csv into xml format and I am using Python for the same. Here is how I started implementing this
for row in csv.reader(open(csvFile)):
if(row): #check for blank lines
if row[0] == 'TYPE':
xmlData.write(' ' + '<TYPE'+ row[1] + '>'+"\n")
elif row[0] == 'TYPESomeTag'
xmlData.write(' ' + '<TYPESomeTag'+ row[2] + '>'+"\n")
elif
#write some more tags
else
#something else
xmlData.close()
This approach that I follow is pretty shabby since its not easily extendible. I am comparing the first column of each row against a string. Now problem arises if there's another set of type definitions like TYPE2. Then I have to write another set of if..else statements which I think is not really the way to do this efficiently.
Could someone advice how can I do the task of converting the above csv to xml in a better way.
EDIT:
This is the xml that I am aiming for:
<tags>
<TYPE Name_1>
<TYPESomeTag>
<id>1</id>
<name>2</name>
<param>3</param>
</TYPESomeTag>
<TYPESomeTag>
<id>4</id>
<name>2</name>
<param>5</param>
</TYPESomeTag>
<TYPEAnothertag>
<param_1>a</param_1>
<param_2>b</param_2>
<param_3>c</param_3>
</TYPEAnothertag>
</TYPE>
<TYPE2 Name_2>
<TYPE2SomeTag>
<id>1</id>
<name>2</name>
<param>3</param>
</TYPE2SomeTag>
<TYPE2SomeTag>
<id>4</id>
<name>2</name>
<param>5</param>
</TYPE2SomeTag>
<TYPE2Anothertag>
<param_1>a</param_1>
<param_2>b</param_2>
<param_3>c</param_3>
</TYPE2Anothertag>
</TYPE2>
</tags>
<TYPE + row[1]and'<TYPESomeTag'+ row[2]. Different index based on different type.