-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path33_country_code.py
43 lines (34 loc) · 1.25 KB
/
33_country_code.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
import csv
import sys
import json
"""
Example usage:
$ python 33_country_code.py 33_sample_csv.csv 33_country_codes.json
"""
def get_data(csv_file, json_file):
countryCodes = []
countryNames = []
continentNames = []
with open(csv_file, "rt") as file_one:
reader = csv.reader(file_one)
with open(json_file) as file_two:
json_data = json.load(file_two)
all_countries = json_data["country"]
for csv_row in reader:
for json_row in all_countries:
if csv_row[0] == json_row["countryCode"]:
countryCodes.append(json_row["countryCode"])
countryNames.append(json_row["countryName"])
continentNames.append(json_row["continentName"])
return [countryCodes, countryNames, continentNames]
def write_data(array_of_arrays):
with open("data.csv", "wt") as csv_out:
writer = csv.writer(csv_out)
rows = zip(array_of_arrays[0], array_of_arrays[1], array_of_arrays[2])
for row in rows:
writer.writerow(row)
if __name__ == "__main__":
csv_file_name = sys.argv[1]
json_file_name = sys.argv[2]
data = get_data(csv_file_name, json_file_name)
write_data(data)