Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e078273

Browse files
authored
Update 33_country_code.py
1 parent 18a2920 commit e078273

File tree

1 file changed

+63
-51
lines changed

1 file changed

+63
-51
lines changed

scripts/33_country_code.py

Lines changed: 63 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,63 @@
1-
import csv
2-
import sys
3-
import json
4-
5-
"""
6-
Example usage:
7-
8-
$ python 33_country_code.py 33_sample_csv.csv 33_country_codes.json
9-
"""
10-
11-
12-
def get_data(csv_file, json_file):
13-
countryCodes = []
14-
countryNames = []
15-
continentNames = []
16-
with open(csv_file, 'rt') as file_one:
17-
reader = csv.reader(file_one)
18-
with open(json_file) as file_two:
19-
json_data = json.load(file_two)
20-
all_countries = json_data["country"]
21-
for csv_row in reader:
22-
for json_row in all_countries:
23-
if csv_row[0] == json_row["countryCode"]:
24-
countryCodes.append(json_row["countryCode"])
25-
countryNames.append(json_row["countryName"])
26-
continentNames.append(json_row["continentName"])
27-
28-
return [
29-
countryCodes,
30-
countryNames,
31-
continentNames
32-
]
33-
34-
35-
def write_data(array_of_arrays):
36-
with open('data.csv', 'wt') as csv_out:
37-
writer = csv.writer(csv_out)
38-
rows = zip(
39-
array_of_arrays[0],
40-
array_of_arrays[1],
41-
array_of_arrays[2]
42-
)
43-
for row in rows:
44-
writer.writerow(row)
45-
46-
47-
if __name__ == '__main__':
48-
csv_file_name = sys.argv[1]
49-
json_file_name = sys.argv[2]
50-
data = get_data(csv_file_name, json_file_name)
51-
write_data(data)
1+
import csv, json
2+
3+
4+
with open("33_country_codes.json","r") as file:
5+
data=json.load(file)
6+
print(data)
7+
8+
9+
10+
11+
12+
13+
14+
15+
codes = {"country": [
16+
{
17+
"countryCode": "AD",
18+
"countryName": "Andorra",
19+
"continentName": "Europe"
20+
},
21+
{
22+
"countryCode": "AE",
23+
"countryName": "United Arab Emirates",
24+
"continentName": "Asia"
25+
},
26+
{
27+
"countryCode": "AF",
28+
"countryName": "Afghanistan",
29+
"continentName": "Asia"
30+
}
31+
]
32+
}
33+
34+
codesc=[]
35+
countries=[]
36+
continrnt=[]
37+
38+
39+
print(codes["country"][0]["countryName"])
40+
41+
for code in data["country"]:
42+
codesc.append(code["countryCode"])
43+
countries.append(code["countryName"])
44+
continrnt.append(code["continentName"])
45+
46+
47+
48+
print(codesc)
49+
print(countries)
50+
print(continrnt)
51+
52+
53+
def save_csv(codesc, countries, continrnt):
54+
with open("myCSV1.csv", "w", newline="") as file:
55+
writer=csv.writer(file)
56+
writer.writerow(["country Code","country Name","continent Name"])
57+
print(codesc,countries,continrnt)
58+
for i in range(1,len(countries)):
59+
writer.writerow([codesc[i],countries[i],continrnt[i]])
60+
61+
62+
save_csv(codesc,countries,continrnt)
63+

0 commit comments

Comments
 (0)