我有一个Json字符串,我可以从中提取一些组件,比如formatted_address
,lat
,lng
,但我无法提取其他组件的特征(值),比如交集、政治、国家、行政区\u级别1,行政区级别2、行政级别3、行政区域级别4、行政区域级别5、口语区、地点、病房、邻居,前提、次优惠等在long_name
下
我希望数据表
formatted_address px_val py_val political country administrative_area_level_1 .. ..
Satya Niwas, Kanti Nagar.. 19.1096591 72.8674712 Kanti Nagar,JB Nagar India maharashtra .. ..
82, Bamanpuri, Ajit Nagar.. 19.109749 72.867249 Bamanpuri India maharashtra .. ..
.
.
.
下面是示例JSON字符串
^{pr2}$这是一段代码
import json
import pandas as pd
line="json_str"
json_st = json.loads(line)
country=[]
political=[]
address_fields = {
'intersection': [],
'political': [],
'country': []
}
for json_str in json_st:
address_fields = {
'intersection': [],
'political': [],
'country': []
}
if isinstance(json_st,dict):
first_address_components = json_st['results']
#format_add = json_st['results'][0]
else:
first_address_components = json_st[0]['address_components']
for item in first_address_components:
for field_key in address_fields.keys():
#address_fields[field_key].append( str(format_add['formatted_address']))
if field_key in item['types']:
address_fields[field_key].append(item['long_name'])
address_fields = {key: ', '.join(values) for key, values in address_fields.items()}
country.append(address_fields['country'])
political.append(address_fields['political'])
它会出错
json_st['results']['address_components']
Traceback (most recent call last):
File "<ipython-input-94-315fa8711f9d>", line 1, in <module>
json_st['results']['address_components']
TypeError: list indices must be integers or slices, not str
我得到了预期O/p的前3列,但无法提取其他列。 关于这个问题的任何建议都会有帮助
谢谢
多姆尼克
这是一个相当宽泛的问题。。。在
要帮助您开始:
结果:
^{pr2}$您需要了解数据的模式。在
json\u st['results']['address_components']
因为json\u st['results']是一个数组
在这里检查一下http://jsoneditoronline.org
这是一些样品
我会选择
^{pr2}$json_normalize
,想到一行的答案,但我不认为这是可能的,即(这里我只为px_val和py_val做过,你可以对其他列做类似的事情)我试图从政治角度来分析这个解决方案,当然不是很自豪
^{3}$输出
result['political']
把它转换成我们能做的方法
相关问题 更多 >
编程相关推荐