共計 573 個字符,預計需要花費 2 分鐘才能閱讀完成。
Python 中可以使用 json
模塊來解析 JSON 文件,并提取數據。下面是一個簡單的示例:
import json
# 讀取 JSON 文件
with open('data.json', 'r') as file:
data = json.load(file)
# 提取數據
name = data['name']
age = data['age']
city = data['address']['city']
print(f"Name: {name}")
print(f"Age: {age}")
print(f"City: {city}")
在這個示例中,我們首先使用 open()
函數打開 JSON 文件,并使用 json.load()
函數將文件內容加載到一個 Python 對象中。
然后,我們可以通過訪問 Python 對象的鍵來提取 JSON 數據。在這個示例中,我們提取了 name
、age
和address
中的 city
字段。
最后,我們使用 print()
函數打印提取到的數據。
請注意,這個示例假設 JSON 文件的結構如下:
{
"name": "John",
"age": 30,
"address": {
"city": "New York"
}
}
你需要根據你的 JSON 文件結構來提取數據。
丸趣 TV 網 – 提供最優質的資源集合!
正文完