python 笔记 之 json对文件的操作

'''
json对文件的操作

'''
import codecs
import json

test = {"a":1,"b":2}
# f = open("1.txt","w")
# json.dump(test,f)  # 写入都文件中

with codecs.open("1.txt","w") as f:
    json.dump(test,f) # python对象写入文件中

with codecs.open("1.txt","r") as f:
    aa = json.load(f) # 从文件中读取为python对象
    print(aa)
    print(type(aa))

猜你喜欢

转载自my.oschina.net/u/3824134/blog/1805303