【发布时间】:2019-07-29 14:09:26
【问题描述】:
我有一个包含 json 数据的列表,如下所示:
txt
["{'type': '点', '坐标': [35.51635659, 139.5662442]}", "{'type': 'Point', 'coordinates': [51.50178423, -0.05362636]}"]
我正在尝试从坐标中提取经纬度,但我真的在为此苦苦挣扎。
当我尝试时:
for each in txt:
print(each)
它返回:
{'type': 'Point', 'coordinates': [35.51635659, 139.5662442]} {'类型':'点','坐标':[51.50178423,-0.05362636]}
当我尝试时:
json_normalize(json.loads(txt))
我收到以下错误:
TypeError Traceback(最近调用 最后)在 ----> 1 json_normalize(json.loads(txt))
C:\ProgramData\Anaconda3\lib\json__init__.py 加载(s,编码, cls,object_hook,parse_float,parse_int,parse_constant, object_pairs_hook, **kw) 339 其他: 340 如果不是 isinstance(s, (bytes, bytearray)): --> 341 raise TypeError(f'JSON对象必须是str, bytes or bytearray, ' 342 f'not {s.class.name}') 第343章 = s.decode(detect_encoding(s), 'surrogatepass')
TypeError:JSON 对象必须是 str、bytes 或 bytearray,而不是 list
如果有人能提供帮助,将不胜感激
谢谢
【问题讨论】:
-
1 for each in txt: ----> 2 print(each['coordinates']) TypeError: string indices must be integers
-
啊,由于某种原因,字典似乎在字符串中:)。为什么它们是字符串?
-
数据原本在一个数据框中,我循环遍历它:for each in cords['geo']: txt.append(each)
-
import ast; for each in txt:..x = ast.literal_eval(each);..print(x['coordinates']) -
为什么
cords['geo']在字符串中? :)
标签: python json dataframe geojson