-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcreate_csv.py
More file actions
78 lines (60 loc) · 3 KB
/
Copy pathcreate_csv.py
File metadata and controls
78 lines (60 loc) · 3 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
## This script takes data from map.json and countries_pop.txt
import csv
import json
# In future directly use https://librarymap.ifla.org/api/map
COUNTRY_DATA = '..//data//countries_pop.txt'
LOOKUP_DATA = '..//data//map.json'
OUTPUT_DATA = '..//data//ifla_data.csv'
OUTPUT_TEXT = '..//data//ifla_text.json'
def read_lookup_data():
"""Return 4 data sets for countries, metrics, languages, and contributors"""
lookups = { 'countries': [], 'metrics': [], 'languages': [], 'contributors': [], 'values': [], 'libraryTypes': [] }
with open(LOOKUP_DATA, encoding='utf-8') as data_file:
data = json.loads(data_file.read())
for idx, item in enumerate(lookups.items()): # For each lookup type
for row in data[item[0]]:
lookups[item[0]].append(row)
return lookups
def run():
"""Main method for creating a single CSV"""
lookups = read_lookup_data()
## Our single CSV will essentially be every country, with each column being associated data taken from other lookups
countries_data = []
set_headers = ['Name']
dynamic_headers = []
for country in lookups['countries']:
# Standard values
country_data = {}
country_data['Name'] = country['name']
# Get all the metric values for the country
country_value_data = {}
for value in lookups['values']:
if value['country_id'] == country['id'] and value['val'] != '-1':
if value['library_type_id'] not in country_value_data:
country_value_data[value['library_type_id']] = {}
country_value_data[value['library_type_id']][value['metric_id']] = value['val']
# Assign the metric values
for metric in lookups['metrics']:
for type in lookups['libraryTypes']:
if metric['name'] and metric['name'] != '' and type['id'] in country_value_data and metric['id'] in country_value_data[type['id']]:
if (type['name'] + ' ' + metric['name']) not in dynamic_headers:
dynamic_headers.append(type['name'] + ' ' + metric['name'])
country_data[type['name'] + ' ' + metric['name']] = country_value_data[type['id']][metric['id']]
countries_data.append(country_data)
headers = set_headers + dynamic_headers
with open(OUTPUT_DATA, 'w', newline='', encoding='utf8') as outputfile:
csvwriter = csv.writer(outputfile, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
final_headers = []
for header in headers:
final_headers.append(header.replace('Number of ', ''))
csvwriter.writerow(final_headers)
for country in countries_data:
country_row = []
if len(country.items()) > 1:
for header in headers:
if header in country:
country_row.append(country[header])
else:
country_row.append('')
csvwriter.writerow(country_row)
run()