3 回答

TA贡献1802条经验 获得超6个赞
使用csv
内置模块。
import csv
json = {
"result" : [{
"conf" : 1.000000,
"end" : 0.300000,
"start" : 0.000000,
"word" : "bright"
}, {
"conf" : 1.000000,
"end" : 0.720000,
"start" : 0.330000,
"word" : "bright"
}, {
"conf" : 1.000000,
"end" : 1.950000,
"start" : 1.710000,
"word" : "bright"
}],
"text" : "bright bright bright"
}
header = json['result'][0].keys()
with open('results.csv', 'w', newline='') as file_:
dict_writer = csv.DictWriter(file_, fieldnames=header)
dict_writer.writeheader()
dict_writer.writerows(json['result'])

TA贡献1836条经验 获得超13个赞
import pandas as pd
json_val = {
"result" : [{
"conf" : 1.000000,
"end" : 0.300000,
"start" : 0.000000,
"word" : "bright"
}, {
"conf" : 1.000000,
"end" : 0.720000,
"start" : 0.330000,
"word" : "bright"
}, {
"conf" : 1.000000,
"end" : 1.950000,
"start" : 1.710000,
"word" : "bright"
}],
"text" : "bright bright bright"
}
pd.read_json(json_val['result'], orient='index').to_csv('someName.csv')

TA贡献1802条经验 获得超5个赞
我会非常推荐pandas
,不会占用很多线路。
根据您的示例,这可以通过以下方式实现:
import pandas as pd pd.read_json(json['result'], orient='index').to_excel('output.xlsx')
添加回答
举报