-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsv2json.py
More file actions
54 lines (40 loc) · 1.25 KB
/
csv2json.py
File metadata and controls
54 lines (40 loc) · 1.25 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
#!/usr/bin/env python3
"""
Description: Converts CSV entries to JSON objects.
Author: P. Sempere (sempere.dev)
"""
import sys
from json import dump
from os import path
OUTPUT_FILE = 'data.json'
HEADERS = ('name', 'family', 'discoverer', 'image')
data = []
files_count = 0
read_count = 0
for file in sys.argv[1:]:
prefix = path.splitext(file)[0]
try:
with open(file, 'r') as fp:
fp.readline() # To remove the headers
for line in fp.readlines():
obj = {
HEADERS[col]: value.replace(';', ',')
for col, value
in enumerate(line.strip('\r\n').split(','))
}
obj['image'] = prefix + '/' + obj['image']
data.append(obj)
read_count += 1
files_count += 1
except IOError:
print(f'Error! Could not read file {file}')
except Exception as e:
print(f'Unexpected error while parsing {file}')
raise e
print(f'Read {read_count} lines from {files_count} files!')
try:
with open('data.json', 'w') as fp:
dump(data, fp)
print(f'All objects written successfully to {OUTPUT_FILE}')
except IOError:
print(f'Error! Could not write to {OUTPUT_FILE}')