数据框“数据集”是由 PowerBI 自动生成的,这是我的结果dataset.head(10).to_clipboard(sep=',', index=False)
coordinates,status
"[143.4865219,-34.7560602]",not started
"[143.4865241,-34.7561332]",not started
"[143.4865264,-34.7562088]",not started
"[143.4865286,-34.7562818]",not started
"[143.4865305,-34.7563453]",not started
"[143.4865327,-34.7564183]",not started
"[143.486535,-34.756494]",not started
"[143.4865371,-34.756567]",not started
"[143.486539,-34.7566304]",not started
"[143.4865412,-34.7567034]",not started
Run Code Online (Sandbox Code Playgroud)
然后获取json
我这样做data=dataset.to_json(orient='records')
这给了我这个结果
[{"coordinates":"[143.4865219,-34.7560602]","status":"not started"},{"coordinates":"[143.4865241,-34.7561332]","status":"not started"},
Run Code Online (Sandbox Code Playgroud)
我如何得到这个,坐标值上没有引号
[{"coordinates":[143.4865219,-34.7560602],"status":"not started"},{"coordinates":[143.4865241,-34.7561332],"status":"not started"},
Run Code Online (Sandbox Code Playgroud)
编辑
print(type(data))
<class 'str'>
Run Code Online (Sandbox Code Playgroud)
您可以使用ast.literal_eval:
安全地评估表达式节点或包含 Python 文字或容器显示的字符串。提供的字符串或节点只能包含以下 Python 文字结构:字符串、字节、数字、元组、列表、字典、集合、布尔值和 None。
这可用于安全地评估包含来自不受信任来源的 Python 值的字符串,而无需自己解析这些值。[...]
您的数据似乎是一个字符串,而不是Python打印的列表(它默认使用单引号,数据中的双引号似乎表明它是一个字符串,可以保存在例如json文件中)。因此,您必须首先使用json.loads将其转换为 Python 对象:
from ast import literal_eval
import json
data = """[{"coordinates":"[143.4865219,-34.7560602]","status":"not started"},{"coordinates":"[143.4865241,-34.7561332]","status":"not started"}]"""
data = json.loads(data)
for d in data:
d['coordinates'] = literal_eval(d['coordinates'])
print(data)
# [{'coordinates': [143.4865219, -34.7560602], 'status': 'not started'}, {'coordinates': [143.4865241, -34.7561332], 'status': 'not started'}]
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2853 次 |
最近记录: |