【发布时间】:2018-10-27 08:01:51
【问题描述】:
我正在使用df.to_json() 将数据帧转换为 json。但它给了我一个 json 字符串而不是一个对象。如何获取 json 对象。
另外,当我将此数据附加到数组时,它会在 json 前后添加单引号,这会破坏 json 结构。如何导出到 json 对象并正确附加。
使用的代码:
a=[]
array.append(df1.to_json(orient='records', lines=True))
array.append(df2.to_json(orient='records', lines=True))
结果:
['{"test:"w","param":1}','{"test:"w2","param":2}]']
要求的结果:
[{"test":"w","param":1},{"test":"w2","param":2}]
【问题讨论】:
-
你能添加一些数据样本吗?
-
你要的不是“json对象”而是字典,使用
df.to_dict()