- 文件序列化—>把服務端的響應數據寫到文件中
json.dump(r.content.decode("utf-8"), open("weather.json", "w"))
- 文件的反序列化—>讀取文件內容
fp=json.load(open("weather.json", "r"))
import json
import requests
"""
文件的序列化與反序列化
"""
r = requests.get(url="http://www.weather.com.cn/data/sk/101190408.html")
# print(r.content.decode("utf-8"))
# 文件序列化--->把服務端的響應數據寫到文件中
json.dump(r.content.decode("utf-8"), open("weather.json", "w"))
"""
1、文件反序列化後,類型是unicode
2、進行編碼,把Unicode類型轉換爲str類型
3、然後使用反序列化把str轉換爲字典類型
"""
# 文件的反序列化--->讀取文件內容
fp=json.load(open("weather.json", "r"))
print(fp,type(fp))
# 把讀取的數據進行反序列化
dict1=json.loads(fp)
print(type(dict1),dict1['weatherinfo']['city'])