-
Notifications
You must be signed in to change notification settings - Fork 0
/
CSVConverter.py
79 lines (58 loc) · 1.81 KB
/
CSVConverter.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import io
import os
import csv
import json
from validators.url import url
filename = "data.csv"
def is_valid_name(name):
if isinstance(name.get('name'), str):
return True
return False
def is_valid_url(uri):
if url(uri.get('uri')):
return True
return False
def is_valid_stars(star):
if 0 <= int(star.get('stars')) <= 5:
return True
return False
def validate_fields(line):
if is_valid_name(line) and is_valid_url(line) and is_valid_stars(line):
return True
return False
def convert_row_to_xml(row):
return f"""<data>
<name>{row.get('name')}</name>
<address>{row.get('address')}</address>
<stars>{row.get('stars')}</stars>
<phone>{row.get('phone')}</phone>
<uri>{row.get('uri')}</uri>
</data>\n"""
def generate_json_file(rows, cwd):
filepath = '\\data.json'
with io.open(cwd + filepath, 'w', encoding='UTF8') as f:
json.dump(rows, f, ensure_ascii=False)
def generate_xml_file(rows, cwd):
filepath = '\\data.xml'
with io.open(cwd + filepath, 'w') as f:
f.write('<dataListings>')
for line in rows:
f.write(convert_row_to_xml(line))
f.write('</dataListings>')
def driver():
cwd = os.getcwd()
if os.path.isfile(filename):
with open(filename) as csv_file:
csv_reader = csv.DictReader(csv_file)
rows = list()
next(csv_reader)
for line in csv_reader:
if line and validate_fields(line):
rows.append(line)
generate_json_file(rows, cwd + '\\data')
generate_xml_file(rows, cwd + '\\data')
if __name__ == '__main__':
print('CSV are being converted to JSON and XML.')
print('....')
driver()
print('data.json and data.xml file generated.')