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
| import json path = 'E:\PYTHON\PydataProject\ch02\data\example.txt' a = open(path).readline() records = [json.loads(line) for line in open(path)] for index,item in enumerate(records, 1): print(index, item) ----------------------------------------------- 3559 {'a': 'GoogleProducer', 'c': 'US', 'nk': 0, 'tz': 'America/Los_Angeles', 'gr': 'CA', 'g': 'zjtI4X', 'h': 'zjtI4X', 'l': 'bitly', 'hh': '1.usa.gov', 'r': 'direct', 'u': 'http://www.ahrq.gov/qual/qitoolkit/', 't': 1331926847, 'hc': 1327528527, 'cy': 'Mountain View', 'll': [37.419201, -122.057404]} 3560 {'a': 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; MS-RTC LM 8; .NET4.0C; .NET4.0E; .NET CLR 1.1.4322)', 'c': 'US', 'nk': 0, 'tz': 'America/New_York', 'gr': 'VA', 'g': 'qxKrTK', 'h': 'qxKrTK', 'l': 'bitly', 'al': 'en-US', 'hh': '1.usa.gov', 'r': 'http://t.co/OEEEvwjU', 'u': 'http://herndon-va.gov/Content/public_safety/Public_Information/weekly_reports.aspx?cnlid=1736', 't': 1331926849, 'hc': 1312897670, 'cy': 'Mc Lean', 'll': [38.935799, -77.162102]} records[:1] Out[8]: [{'a': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.78 Safari/535.11', 'al': 'en-US,en;q=0.8', 'c': 'US', 'cy': 'Danvers', 'g': 'A6qOVH', 'gr': 'MA', 'h': 'wfLQtf', 'hc': 1331822918, 'hh': '1.usa.gov', 'l': 'orofrog', 'll': [42.576698, -70.954903], 'nk': 1, 'r': 'http://www.facebook.com/l/7AQEFzjSi/1.usa.gov/wfLQtf', 't': 1331923247, 'tz': 'America/New_York', 'u': 'http://www.ncbi.nlm.nih.gov/pubmed/22415991'}]
|