-
Notifications
You must be signed in to change notification settings - Fork 0
/
3_convert_to_xml.py
45 lines (33 loc) · 1.23 KB
/
3_convert_to_xml.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import pandas as pd
import os
directory = os.path.dirname(os.path.abspath(__file__))
os.chdir(directory)
csvFile = 'gaiadata.csv'
xmlFile = 'gaiadatatest.xml'
def ConvertToXML():
csvData = pd.read_csv(csvFile, sep=',')
nrows = len(csvData.index)
print('Number of rows: ', nrows)
xmlData = open(xmlFile, 'w')
xmlData.write('<?xml version="1.0"?>' + '\n')
xmlData.write('<RESOURCE>' + '\n')
xmlData.write('<TABLE name= "' + directory + '" nrows=>"' + str(nrows) +'">' + '\n')
for field in csvData.columns.values:
dtype = csvData[field].dtype
xmlData.write('<FIELD datatype="' + str(dtype) + '" name="' + str(field) + '"/>' '\n')
xmlData.write('<DATA>' + '\n')
xmlData.write('<TABLEDATA>' + '\n')
cols = csvData.columns.values
matrix = csvData.as_matrix()
rowNum = 0
for row in range(nrows):
xmlData.write(' <TR>' + '\n')
for i in range(len(cols)):
xmlData.write(' <TD>' + str(matrix[rowNum, i]) + '</TD>' + '\n')
xmlData.write(' </TR>' + '\n')
xmlData.write('</TABLEDATA>' + '\n')
xmlData.write('</DATA>' + '\n')
xmlData.write('</TABLE>' + '\n')
xmlData.write('</RESOURCE>' + '\n')
xmlData.close()
ConvertToXML()