-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert_xml2csv_v2.py
More file actions
64 lines (53 loc) · 1.85 KB
/
convert_xml2csv_v2.py
File metadata and controls
64 lines (53 loc) · 1.85 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from xml.etree import ElementTree
import csv
# PARSE XML
xml = ElementTree.parse(".\sample_response.txt")
row_fields = [ "race_id",
"name",
"description",
"url",
"external_race_url",
"fb_page_id",
"last_date",
"last_end_date",
"next_date",
"next_end_date",
"is_draft_race",
"is_private_race",
"is_registration_open",
"created",
"last_modified",
"./address/street",
"./address/street2",
"./address/city",
"./address/state",
"./address/zipcode",
"./address/country_code",
"timezone",
"real_time_notifications_enabled"]
# CREATE CSV FILE
csvfile = open("data.csv",'w',newline='',encoding='utf-8')
csvfile_writer = csv.writer(csvfile)
# creating the header to csv file
csvfile_writer.writerow(row_fields)
# for each RACE
for race in xml.findall("race"):
line_data = []
if(race):
# extract RACE details
for race_field in row_fields:
tmp_field = race.find(race_field)
if tmp_field is not None :
if tmp_field.text is not None :
tmp_field = tmp_field.text
tmp_field = tmp_field.replace('\n',"")
tmp_field = tmp_field.replace('\t',"")
tmp_field = tmp_field.replace(';',"")
else:
tmp_field = "\"\""
else :
tmp_field = "(field not present)"
line_data.append(tmp_field)
# add new row to csv file
csvfile_writer.writerow(line_data)
csvfile.close()