在Python

时间:2018-06-08 00:09:27

标签: json python-3.x

因此,我必须返回每个唯一的位置(cf. city)以及它从JSON文件中出现的频率。我将JSON文件上传到python中,但我很难返回该地点及其出现位置。

继承我将json导入我的python代码的代码:

import pandas as pd
import json
with open('data.json') as json_file:      
   json_file = json_file.readlines()
   json_file = list(map(json.loads, json_file))

继承人json数据文件的第一行:

{'payload': {'existence_full': 1,
  'geo_virtual': '["56.9459720|-2.1971226|20|within_50m|4"]',
  'latitude': '56.945972',
  'locality': 'Stonehaven',
  '_records_touched': '{"crawl":8,"lssi":0,"polygon_centroid":0,"geocoder":0,"user_submission":0,"tdc":0,"gov":0}',
  'address': 'The Lodge, Dunottar',
  'email': 'dunnottarcastle@btconnect.com',
  'existence_ml': 0.5694238217658721,
  'domain_aggregate': '',
  'name': 'Dunnottar Castle',
  'search_tags': ['Dunnottar Castle Aberdeenshire', 'Dunotter Castle'],
  'admin_region': 'Scotland',
  'existence': 1,
  'category_labels': [['Landmarks', 'Buildings and Structures']],
  'post_town': 'Stonehaven',
  'region': 'Kincardineshire',
  'review_count': '719',
  'geocode_level': 'within_50m',
  'tel': '01569 762173',
  'placerank': 65,
  'longitude': '-2.197123',
  'placerank_ml': 37.27916073464469,
  'fax': '01330 860325',
  'category_ids_text_search': '',
  'website': 'http://www.dunnottarcastle.co.uk',
  'status': '1',
  'geocode_confidence': '20',
  'postcode': 'AB39 2TL',
  'category_ids': [108],
  'country': 'gb',
  '_geocode_quality': '4'},
 'uuid': '3867aaf3-12ab-434f-b12b-5d627b3359c3'}

我写了这个和许多其他代码,但我不断得到错误,我必须调用整数而不是字符串:

for i in range (0, len (json_file)):
    print (json_file['paylod'][i]['locality'])

1 个答案:

答案 0 :(得分:0)

不要使用图书馆" json" 使用:泡菜更快,效果更好

这应该有效:

 example = open(filename, 'rb')
 toprint = _pickle.load(example, encoding='latin1')
 print(toprint)