<style> .reveal { font-size: 30px; } .reveal h1 { font-size: 42px; } </style> # NCKU ES Python 視覺化 - 檔案讀取 ###### tags:`NCKU_ES` `python lecture` --- # JSON Introduction What is JSON? >JSON stands for **JavaScript Object Notation** JSON is a lightweight format for storing and transporting data JSON is often used when data is sent from a server to a web page JSON is "self-describing" and easy to understand [Citation](https://www.w3schools.com/whatis/whatis_json.asp) ---- ## Example ![](https://media.geeksforgeeks.org/wp-content/uploads/20191217135502/pyhton-append-json.png) ---- ## 檔案讀取 ```python= f = open(filename, mode) ``` Mode - r:(read)唯讀模式 - w:(write)寫入模式(覆寫) - a:(append)寫入模式(續寫) ---- ## Sample Data ```json= { "people":[{ "name": "Bob", "languages": "English" }, { "name": "Yen", "languages": "Chinese" } ] } ``` ---- ## Example ```python= import json # Opening JSON file f = open('data.json', 'r') # returns JSON object as a dictionary data = json.load(f) print("Raw data:") print(data) print("Object:") # Iterating through the json list for i in data['people']: print(i) # Closing file f.close() ``` --- # CSV Introduction ---- ## Intro 逗號分隔值(Comma-Separated Values,CSV),以純文字記錄檔案資訊,並以特定自元作為資料分隔 ### 常用於 dataset 資料儲存 [Open dataset](https://www.kaggle.com/datasets) ---- ## Python 常用的 csv 操作工具 ### csv [csv lib](https://docs.python.org/3/library/csv.html) ### pandas [pandas](https://pandas.pydata.org/getting_started.html) ---- ## Example ```python= import csv # 開啟 CSV 檔案 with open('iris.csv', newline='') as csvfile: # 讀取 CSV 檔案內容 rows = csv.reader(csvfile) # 以迴圈輸出每一列 for row in rows: print(row) ```
{"metaMigratedAt":"2023-06-15T15:25:55.715Z","metaMigratedFrom":"YAML","title":"NCKU ES Python 部課 - 檔案讀取","breaks":true,"slideOptions":"{\"theme\":\"League\",\"transition\":\"fade\",\"spotlight\":{\"enabled\":false},\"slideNumber\":true,\"parallaxBackgroundImage\":\"https://wallpaperboat.com/wp-content/uploads/2019/10/programming-02.jpg\"}","contributors":"[{\"id\":\"af67681e-fb2e-4298-8e92-448b36d346a6\",\"add\":2265,\"del\":241}]"}
    337 views