In [1]: path = 'usagov_bitly_data2012-03-16-1331923249.txt'
open(path).readline()
# file contains a web data known as JSON-JavaScript Object Notation
# output JSON string
out[1]: '{ "a": "Mozilla\/5.0 (Windows NT 6.1; WOW64) AppleWebKit\/535.11 (KHTML, like Gecko) Chrome\/17.0.963.78 Safari\/535.11", "c": "US", "nk": 1, "tz": "America\/New_York", "gr": "MA", "g": "A6qOVH", "h": "wfLQtf", "l": "orofrog", "al": "en-US,en;q=0.8", "hh": "1.usa.gov", "r": "http:\/\/www.facebook.com\/l\/7AQEFzjSi\/1.usa.gov\/wfLQtf", "u": "http:\/\/www.ncbi.nlm.nih.gov\/pubmed\/22415991", "t": 1331923247, "hc": 1331822918, "cy": "Danvers", "ll": [ 42.576698, -70.954903 ] }\n'
In [2]: import json
# use loads function in json module to convert a JSON string into a Python dictionary object
path = 'usagov_bitly_data2012-03-16-1331923249.txt'
records = [json.loads(line) for line in open(path)]
# list comprehension, returns a list of dictionaries
print(records[0])
print(records[0]['tz'])
Out[2]:{'h': 'wfLQtf', 'gr': 'MA', 'u': 'http://www.ncbi.nlm.nih.gov/pubmed/22415991', 'l': 'orofrog', 'tz': 'America/New_York', 'c': 'US', 'al': 'en-US,en;q=0.8', 'hc': 1331822918, 'hh': '1.usa.gov', 'cy': 'Danvers', 'nk': 1, 'a': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.78 Safari/535.11', 'r': 'http://www.facebook.com/l/7AQEFzjSi/1.usa.gov/wfLQtf', 't': 1331923247, 'g': 'A6qOVH', 'll': [42.576698, -70.954903]}
America/New_York
In [3]: time_zones = [tz['tz']for tz in records if 'tz' in tz]
time_zones_withoutms = [tz['tz']for tz in records if 'tz' in tz and tz['tz'] is not '']
# the time zone list without missing data
print(time_zones[:11])
print(time_zones_withoutms[:11])
Out[4]: ['America/New_York', 'America/Denver', 'America/New_York', 'America/Sao_Paulo', 'America/New_York', 'America/New_York', 'Europe/Warsaw', '', '', '', 'America/Los_Angeles']
['America/New_York', 'America/Denver', 'America/New_York', 'America/Sao_Paulo', 'America/New_York', 'America/New_York', 'Europe/Warsaw', 'America/Los_Angeles', 'America/New_York', 'America/New_York', 'America/New_York']
In [4]: from collections import defaultdict
def counts(s):
"""produce how many times a certain kinda variable occures.
s: a sequence that can be iterated
returns: counting numbers
"""
countings = defaultdict(int)
# the values are initialized to 0
for x in s:
countings[x] += 1
return countings
countings = counts(time_zones)
print(countings['America/New_York'])
print(len(time_zones))
Out[4]: 1251
3440
In [5]: print(countings)
Out[5]: defaultdict(<class 'int'>, {'': 521, 'America/Halifax': 4, 'America/Recife': 2, 'Asia/Novosibirsk': 1, 'America/Montreal': 9, 'Europe/Sofia': 1, 'America/New_York': 1251, 'Europe/Riga': 2, 'America/Argentina/Buenos_Aires': 1, 'Europe/Moscow': 10, 'America/Guayaquil': 2, 'America/Winnipeg': 4, 'America/La_Paz': 1, 'Asia/Karachi': 3, 'America/Santo_Domingo': 1, 'Europe/Uzhgorod': 1, 'America/Sao_Paulo': 33, 'Europe/Madrid': 35, 'Africa/Johannesburg': 1, 'Asia/Pontianak': 1, 'Chile/Continental': 6, 'America/Argentina/Mendoza': 1, 'Europe/Prague': 10, 'America/Tegucigalpa': 1, 'America/Phoenix': 20, 'Asia/Dubai': 4, 'America/Rainy_River': 25, 'Asia/Bangkok': 6, 'Asia/Yekaterinburg': 1, 'America/Vancouver': 12, 'America/Montevideo': 1, 'America/Edmonton': 6, 'America/Argentina/Cordoba': 1, 'Australia/NSW': 6, 'Asia/Jakarta': 3, 'America/Costa_Rica': 1, 'America/Denver': 191, 'Europe/Berlin': 28, 'America/Lima': 1, 'Pacific/Honolulu': 36, 'Europe/Ljubljana': 1, 'Europe/Paris': 14, 'Europe/Athens': 6, 'Europe/Belgrade': 2, 'Pacific/Auckland': 11, 'Australia/Queensland': 1, 'Europe/Skopje': 1, 'Europe/Bratislava': 3, 'America/Anchorage': 5, 'Europe/Lisbon': 8, 'Europe/Amsterdam': 22, 'Europe/Vilnius': 2, 'Asia/Amman': 2, 'Europe/Bucharest': 4, 'America/Puerto_Rico': 10, 'Europe/Stockholm': 14, 'America/Managua': 3, 'America/Caracas': 1, 'Europe/Oslo': 10, 'Asia/Manila': 1, 'America/Chicago': 400, 'Europe/London': 74, 'Africa/Lusaka': 1, 'Europe/Brussels': 4, 'Europe/Volgograd': 1, 'Europe/Copenhagen': 5, 'Asia/Harbin': 3, 'America/Bogota': 3, 'Europe/Malta': 2, 'Europe/Rome': 27, 'Asia/Tokyo': 37, 'Europe/Vienna': 6, 'Africa/Ceuta': 2, 'Africa/Cairo': 3, 'Asia/Jerusalem': 3, 'Europe/Budapest': 5, 'America/Mexico_City': 15, 'Asia/Kuching': 1, 'Asia/Beirut': 4, 'Asia/Seoul': 5, 'Africa/Casablanca': 1, 'America/Monterrey': 1, 'Asia/Kuala_Lumpur': 3, 'Asia/Istanbul': 9, 'Asia/Calcutta': 9, 'America/Indianapolis': 20, 'America/St_Kitts': 1, 'America/Los_Angeles': 382, 'Asia/Riyadh': 1, 'Europe/Warsaw': 16, 'America/Chihuahua': 2, 'Europe/Dublin': 3, 'Europe/Helsinki': 10, 'Asia/Nicosia': 1, 'America/Mazatlan': 1, 'Asia/Hong_Kong': 10, 'Europe/Zurich': 4})
dictionary is an object without order, so if we wanna top 10 time zones and their countings we have to use another data structure