JSON(JavaScript Object Notation)是一个轻量级的数据交换格式,Python标准库json完美实现了该格式,用法类似于marshal和pickle。...json.JSONEncoder):
def default(self, obj):
if isinstance(obj, set):
return list(obj)
return json.JSONEncoder.default...json.JSONDecoder.decode(self, obj))
# 使用自定义的编码器和解码器
>>> json.dumps({1,2,3,4}, cls=setEncoder)
'[1, 2,...》(第2版)、
《Python可以这样学》
清华大学出版社'''
# 将内容序列化并写入文本文件
>>> with open('test.txt', 'w') as fp:
json.dump(...s, fp)
# 读取文件内容并反序列化
>>> with open('test.txt') as fp:
print(json.load(fp))