Python:JSon输出到文件(Python : JSon Output to a file)
我从我的Python脚本输出一个JSon格式,我想把JSon写入一个文件。
我用
df_json.to_json(orient='records')
with open('JSONData.json', 'w') as f:
json.dump(df_json, f)
我有以下错误:
raise TypeError(repr(o) + " is not JSON serializable")
[1746 rows x 2 columns] is not JSON serializable
我不知道我做错了什么。
我的JSon输出如下:
[
{
"id": 1,
"results": [
1,
2,
3
]
},
{
"id": 558599,
"results": [
4,
5,
6
]
}
]
先感谢您。
I have as an output from my Python script a JSon format and I want to write the JSon into a file.
I use
df_json.to_json(orient='records')
with open('JSONData.json', 'w') as f:
json.dump(df_json, f)
I have the following error:
raise TypeError(repr(o) + " is not JSON serializable")
[1746 rows x 2 columns] is not JSON serializable
I don't know what I am doing wrong.
My JSon output is as follows :
[
{
"id": 1,
"results": [
1,
2,
3
]
},
{
"id": 558599,
"results": [
4,
5,
6
]
}
]
Thank you in advance.
原文:https://stackoverflow.com/questions/42439785
2020-09-13 12:09
满意答案
你正在调用df_json.to_json(orient='records')但你没有使用结果。 Object不会将自身变异为jsonisable对象。
序列化原始对象显然不起作用(或者不会有to_json方法)
由于字符串已经是json