标签:file write imp 排序 变量 print read serialize with open
import simplejson import anyjson # 序列化: 把变量从内存中变成可存储或可传输的过程 def any_1(): # 序列化 info = {"a": 1, "b": 2} result = anyjson.serialize(info) f = open("file.text", "w") f.write(result) f.close() def any_2(): # 反序列化 with open("file.text", "r") as f: info = anyjson.deserialize(f.read()) print(info) def smp_1(): # 序列化 # 方式一 """info = {"a":1, "b": 2} result = simplejson.dumps(info) with open("122.text", "w") as f: f.write(result)""" # 方式二 info = {"a": 1, "b": 2} with open("122.text", "w") as f: simplejson.dump(info, f) def simp_2(): # 反序列化 # 方式一 """with open("122.text", "r") as f: info = f.read() result = simplejson.loads(info) print(result)""" # 方式二 with open("122.text", "r") as f: info = simplejson.load(f) print(info) # 排序 a = {"c": 1, "b": 2} info = simplejson.dumps(a, sort_keys=True) print(info) # {"b": 2, "c": 1}
标签:file write imp 排序 变量 print read serialize with open
原文地址:https://www.cnblogs.com/zhouzetian/p/13166245.html