-
Notifications
You must be signed in to change notification settings - Fork 33
/
population.py
executable file
·89 lines (69 loc) · 2.3 KB
/
population.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
80
81
82
83
84
85
86
87
88
import json
import world_data
with open('country-by-population.json') as f:
countryByPopulationJson = json.loads(f.read())
countryPopulation = {}
for item in countryByPopulationJson:
if not item['population']:
continue
countryPopulation[item['country']] = int(item['population'])
cp2 = {}
# some fixes
countryPopulation['US'] = countryPopulation['United States']
countryPopulation['Czechia'] = countryPopulation["Czech Republic"]
countryPopulation['Korea, South'] = countryPopulation['South Korea']
countryPopulation['Korea, North'] = countryPopulation['North Korea']
countryPopulation['Taiwan*'] = 23574274
countryPopulation['Serbia'] = 7057666
cp2['Hubei'] = 59E6
# ...
countries, provinces = world_data.get_countries_provinces()
for country in countries:
if country in countryPopulation:
cp2[country] = countryPopulation[country]
else:
for country2 in countryPopulation:
if country in country2:
cp2[country] = countryPopulation[country2]
def get_population(country, province='all', excludeCountries=[]):
if province != 'all':
country = province
if country == 'all':
p = 7.8E9
for e in excludeCountries:
p -= get_population(e, 'all')
return p
if not country in cp2:
countries, provinces = world_data.get_countries_provinces()
print('\n', countries, '\n\n', provinces)
raise Exception('Country / province not found. See above.')
return cp2[country]
def get_all_population_data():
return cp2
if __name__ == '__main__':
for country in countries:
if not country in cp2:
print(country)
s = ''
for c in countryPopulation.keys():
if not c in countries:
s += " %s " % c
print(s)
# region population from wikipedia
# import wptools
# def get_population(region):
# page = wptools.page(region)
# page.get_parse(show=True)
# global infobox
# infobox = page.data['infobox']
# for s in ['population_estimate', 'population']:
# try:
# population = infobox[s]
# continue
# except KeyError:
# pass
# print(region + " p: " + str(population))
# return population
#for country in countries:
# get_population(country)
#get_population('China')