Y
Y
Yojhik2015-10-21 00:14:22
Python
Yojhik, 2015-10-21 00:14:22

How to convert CSV file to XML by structure using Python?

Good day to all community members. I really need help converting CSV file to XML using Python.
There is a script in Python that works great with only one blot, it substitutes the name of the columns (all in a row) into the XML tags, but you need to select only a few so that it replaces the tags by column number. In other words, there is a 13th column and it is necessary that in the XML output it be called , as well as for the 16th . Please tell me, I think it’s not so difficult for Toster participants, but it will be much easier for me to figure it out with a hint.
Attached script:
import csv

csvFile = 'Data.csv'
xmlFile = 'Data.xml'
csvData = csv.reader(open(csvFile))
xmlData = open(xmlFile, 'w')
xmlData.write('<?xml version="1.0"?> ' + "\n")
# there must be only one top-level tag
xmlData.write('' + "\n")
rowNum = 0
for row in csvData:
if rowNum == 0:
tags = row
for i in range (len(tags)):
tags[i] = tags[i].replace(' ', '_')
else:
xmlData.write('' + "\n")
for i in range(len(tags)) :
xmlData.write(' ' + '<' + tags[i] + '>'\
+ row[i] + '' + tags[i] + '>' + "\n")
xmlData.write('' + "\n")
rowNum +=1
xmlData.write('' + "\n")
xmlData.close()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yojhik, 2015-10-21
@Yojhik

The task is to display columns 13 and 16 with the assigned names.
In general, there are 30 columns in the file, you need to select about 10 of them and assign your own names to each of the 10.
So in the file there is a 13th column which has the name oblast, but it is necessary that in xml it be in the area tags.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question