python输出json到文件_Python:JSon输出到文件(Python : JSon Output to a file)

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 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值