从 Python for Excel 中的 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"

}

我有这个 JSON 数组。我需要从表格格式的“结果”中提取所有详细信息。例如,


   conf       start      end        word

1.000000    0.000000   0.300000    bright

1.000000    0.330000   0.720000    bright

1.000000    1.710000   1.950000    bright

如何从“结果”部分提取这些值并将详细信息附加到 excel 中?


当年话下
浏览 145回答 3
3回答

呼啦一阵风

使用csv内置模块。import csvjson = {  "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'])

开心每一天1111

import pandas as pdjson_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')

12345678_0001

我会非常推荐pandas,不会占用很多线路。根据您的示例,这可以通过以下方式实现:import pandas as pd pd.read_json(json['result'], orient='index').to_excel('output.xlsx')
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python