Python實現讀取json檔案到excel表

來源:互聯網
上載者:User
這篇文章主要介紹了Python實現讀取json檔案到excel表,具有一定的參考價值,感興趣的小夥伴們可以參考一下

本文執行個體為大家分享了Python實現讀取json檔案到excel表,供大家參考,具體內容如下

一、需求

1、'score.json' 檔案內容:

{  "1":["小花",99,100,98.5],  "2":["小王",90,30.5,95],  "3":["小明",67.5,49.6,88]}

2、讀取json檔案儲存到資料庫,並計算出每個人的總分和平均分

二、實現代碼

import json, xlwtdef read_score(jsonfile):  with open(jsonfile, encoding='utf-8') as f: # 將json檔案轉化為字典    score_all = json.load(f)  book = xlwt.Workbook() # 建立excel檔案  sheet = book.add_sheet('sheet1') # 建立一個表  title = ['序號', '姓名', '語文', '數學', '英語', '總分', '平均分']  for col in range(len(title)): # 存入第一列名    sheet.write(0, col, title[col])  row = 1 # 定義行  for k in score_all:    data = score_all[k] # data儲存姓名和分數的list    data.append(sum(data[1:4])) # 倒數第二列加入總分    data.append(sum(data[1:4]) / 3.0) # 最後一列加入平均分    data.insert(0, k) # 第一列加入序號    for index in range(len(data)): # 依次寫入每一行      sheet.write(row, index, data[index])    row += 1  book.save('score.xls')read_score('score.json')

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.