亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從 Python for Excel 中的 json 數組中提取數據

從 Python for Excel 中的 json 數組中提取數據

當年話下 2023-06-06 15:03:00
{  "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        word1.000000    0.000000   0.300000    bright1.000000    0.330000   0.720000    bright1.000000    1.710000   1.950000    bright如何從“結果”部分提取這些值并將詳細信息附加到 excel 中?
查看完整描述

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'])


查看完整回答
反對 回復 2023-06-06
?
開心每一天1111

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')


查看完整回答
反對 回復 2023-06-06
?
12345678_0001

TA貢獻1802條經驗 獲得超5個贊

我會非常推薦pandas,不會占用很多線路。

根據您的示例,這可以通過以下方式實現:

import?pandas?as?pd
pd.read_json(json['result'],?orient='index').to_excel('output.xlsx')
查看完整回答
反對 回復 2023-06-06
  • 3 回答
  • 0 關注
  • 173 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號