iT邦幫忙

1

工程師菜鳥_宇弟的 學習筆記 [Python] 10 - [Python] [Json] 進階說明 二 , 結合excel 怎麼用

  • 分享至 

  • xImage
  •  

[Python] 之旅第10天

實用練習

  • 練習1- [Json] 有三個ID資料,可以刪一個user

  • 練習2- [Json] [Python] [Excel] 資料的讀,寫


練習1- [Json] 有三個ID資料,可以刪一個user

import json
o = json.loads("""[
    {
        "ID": 1 ,                # 紀錄第一筆資料
        "name": "mark",
        "url": "fbfunny.com",
        "number": "09111111",
        "gender": "male"
    },
    {
        "ID": 2 ,                # 紀錄第二筆資料
        "name": "joy",
        "url": "Lennon.com",
        "number": "0922222",
        "gender": "female"
    }
    ,
    {
        "ID": 3 ,                # 紀錄第三筆資料
        "name": "penny",
        "url": "penny.com",
        "number": "093333",
        "gender": "female"
    }
]""")
 
for idx, dictionary in enumerate(o):       # 閱覽所有 "o" 裡的資料
    if dictionary['ID'] == 1:              # 如果 id等於1, 就刪除
        o.pop(idx)
 
 
o2 = json.dumps(o)
print(o2)

成果會長這樣喔:
https://ithelp.ithome.com.tw/upload/images/20221116/201530341CyOjaekQz.jpg

原本有三筆資料 對應 ID: 1, ID: 2, ID: 3
當我再把資料印出時,確實少了 ID: 1 的資料


練習2- [Json] [Python] [Excel] 資料的讀,寫

1.READ WRITE , 從URL 抓日期在Excel 裡呈現
2. READ WRITE , 在Excel 讀資料

相關連結:
Convert JSON to Excel using json2excel Library in Python
https://www.youtube.com/watch?v=gop5KuQiE_o&ab_channel=CodingDiksha

  • 假設我用python 在編輯器上記錄下 json格式 的資料,要將其寫入成excel的方法,為下列示範
 # 使用工具包 "json2excel"
from json2excel import Json2Excel
 
if __name__ == '__main__':
    json2excel = Json2Excel(head_name_cols=["rank", "name"])    # 把"rank"跟"name"作為首列
    # print(json2excel.run('./test.json'))
 
    jsons = [
        {
            "rank": 1,               # 紀錄第一筆資料
            "student_no": 1001,
            "name": "Joy",
            "score": 999999,
            "class": "A",
           
        },
        {
           
            "name": "happy",          # 紀錄第二筆資料
            "student_no": 1002,
            "score": 91,
            "class": "B",
            "rank": 2
        },
    ]
    print(json2excel.run(jsons))

成果會長這樣喔:
https://ithelp.ithome.com.tw/upload/images/20221116/20153034jqNCGsLcoy.jpg

可以看到在編輯器上寫的資料,也能寫入並存在excel 裡


今天就先醬 > _ <

其他貼文~~
(變強,就從小小的累積開始)

[Python] 之旅第1天 - python環境
https://ithelp.ithome.com.tw/articles/10296280

[Python] 之旅第2天-用 [Python] 跟 [Flask] 為基礎,把 URL input 抓出來
https://ithelp.ithome.com.tw/articles/10296290

[Python] 之旅第3天- 用 [Python] 跟 [Flask]
https://ithelp.ithome.com.tw/articles/10296965

[Python] 之旅第4天- [Python] [Flask] 的應用
https://ithelp.ithome.com.tw/articles/10303948

[Python] 之旅第5天- [Python] [Flask] 的應用, 日期計算 及日期報錯篇
https://ithelp.ithome.com.tw/articles/10309202

[Python] 之旅第6天- [Python] [Flask] 的應用, 比較有小數點圓的面積大小,簡易請假系統製作
https://ithelp.ithome.com.tw/articles/10309352

[Python] 之旅第7天- [Python] [regular expression] 的應用,找特定資料,或限制輸入的格式
https://ithelp.ithome.com.tw/articles/10309371

[Python] 之旅第8天 - [Python] [regular expression] 的應用, 甚麼是 Json
https://ithelp.ithome.com.tw/articles/10309422

[Python] 之旅第9天 - [Python] [Json] 進階說明
https://ithelp.ithome.com.tw/articles/10309620

[Python] 之旅第10天 - [Python] [Json] 進階說明 二 , 結合excel 怎麼用
https://ithelp.ithome.com.tw/articles/10309850


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言