代码示例库
搜索、筛选并收藏你需要的 Python 代码片段。
#入门
#字符串
#数字
#列表
#列表
#循环
#切片
#if
#列表
#字典
#字典
#while
#while
#函数
#函数
#类
#类
#文件
#异常
#测试
#测试
#Pygame
#Pygame
#matplotlib
#随机漫步
#CSV
#API
#Django
#Django
#Django
异常与json
第10章 文件和异常演示try-except与json存储。
import json
try:
print(5 / 0)
except ZeroDivisionError:
print("不能除以零!")
else:
print("计算成功")
# json 存储
numbers = [1, 2, 3, 4, 5]
with open("numbers.json", "w") as f:
json.dump(numbers, f)
with open("numbers.json") as f:
loaded = json.load(f)
print(loaded)