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')
添加回答
舉報