Created
July 11, 2016 12:41
-
-
Save akx/9cd2145eecf4c7bf48dc78491ef21562 to your computer and use it in GitHub Desktop.
parse_migr_resvas.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # download: http://ec.europa.eu/eurostat/estat-navtree-portlet-prod/BulkDownloadListing?file=data/migr_resvas.tsv.gz | |
| import gzip | |
| from collections import defaultdict | |
| geo_to_citizen = defaultdict(dict) | |
| for line in gzip.GzipFile("migr_resvas.tsv.gz"): | |
| line = line.decode("utf8") | |
| if not line.startswith("T,TOTAL"): # Ignore non-total lines | |
| continue | |
| bits = line.split("\t") | |
| sex, age, citizen, unit, geo = bits.pop(0).split(",") # Pop and split the key | |
| if citizen == "TOTAL": # Ignore the citizen-total lines | |
| continue | |
| valid_numbers = [int(n.strip()) for n in bits if n.strip() != ":"] # ignore "data missing"; will be in newest-to-oldest order | |
| geo_to_citizen[geo][citizen] = valid_numbers[0] | |
| for geo, citizen_data in sorted(geo_to_citizen.items()): | |
| print (geo, max(citizen_data.items(), key=lambda p: p[1])) |
Author
akx
commented
Jul 11, 2016
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment