Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中打印JSON文件中的元素_Json_Python 3.x - Fatal编程技术网

在Python中打印JSON文件中的元素

在Python中打印JSON文件中的元素,json,python-3.x,Json,Python 3.x,因此,我必须从JSON文件返回每个唯一的位置(参见city)以及它发生的频率。我将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

因此,我必须从JSON文件返回每个唯一的位置(参见city)以及它发生的频率。我将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'])
不要使用库“json” 用途:泡菜更快,效果更好

这应该起作用:

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

您的代码假设
json\u文件['payload']
是一个数组(p.s.-im假设您的意思是payload而不是paylod),而您的json显示键
payload
不是一个数组,因此您在其中编写了“\u pickle.load(例如,encoding='latin1')”我是否编写了'locality',例如,它告诉我所有的locality?您所指的locality是什么,是唯一编码为文件的字符集