如何通过pandas从mysql读取json数据?

quhf5bfb  于 2023-08-01  发布在  Mysql
关注(0)|答案(1)|浏览(115)

我在mysql db列中有这种类型的json,我想使用python读取并转换为CSV。

{"type":["11","27","26","6"],
 "comment":["","","","ADVANCE"],
 "remark":["","","","anything"],
 "unit":["1.00","1.00","1.00","5000"],
 "rate":["1300000","1409.37","100","1"],
 "extra":["1.00","850","850","1"],
 "amount":["1300000.00","1197964.50","85000.00","5000.00"]}

字符串
我需要像这样的最终输出:

"type","comment","remark","unit","rate","extra","amount"
"11","","","1.00","1300000","1.00","1300000.00"
"27","","","1.00","1409.37","850","1197964.50"
"26","","","1.00","100","850","85000.00"
"6","ADVANCE","anything","5000","1","1","5000.00"

e4yzc0pl

e4yzc0pl1#

好吧,通过使用pandas和json模块,我们可能有一个解决方案。这是从原始数据到csv文件的代码:

import pandas as pd
import json

data = {"type":["11","27","26","6"],
 "comment":["","","","ADVANCE"],
 "remark":["","","","anything"],
 "unit":["1.00","1.00","1.00","5000"],
 "rate":["1300000","1409.37","100","1"],
 "extra":["1.00","850","850","1"],
 "amount":["1300000.00","1197964.50","85000.00","5000.00"]}

json_object = json.dumps(data, indent=1)
results = pd.read_json(json_object)

results.to_csv(path_or_buf = 'location\\data_json.csv', index = False, encoding = 'UTF-8', sep = ',')

字符串
输出量:

type,comment,remark,unit,rate,extra,amount
11,,,1,1300000.0,1,1300000.0
27,,,1,1409.37,850,1197964.5
26,,,1,100.0,850,85000.0
6,ADVANCE,anything,5000,1.0,1,5000.0


希望对你有帮助。如果不是你想要的,请给予更多的信息或上下文。美好的一天

相关问题