首页 > 编程语言 > 详细

Python基础-序列化 json,pickle

时间:2020-05-07 22:50:22      阅读:51      评论:0      收藏:0      [点我收藏+]

json中duamps

 

# 特殊的参数
l1 = [1, 2, 3, {name: alex}]

# dump load 只能写入文件,只能写入一个数据结构
# with open(‘json文件1‘,encoding=‘utf-8‘,mode=‘w‘) as f1:
#     json.dump(l1,f1)

# 读取数据
# with open(‘json文件1‘,encoding=‘utf-8‘) as f2:
#     l1 = json.load(f2)
#     print(l1,type(l1))


# 一次写入文件多个数据怎么做?

# 错误示例:
# dic1 = {‘username‘: ‘alex‘}
# dic2 = {‘username‘: ‘太白‘}
# dic3 = {‘username‘: ‘大壮‘}
# with open(‘json文件1‘,encoding=‘utf-8‘,mode=‘w‘) as f1:
#     json.dump(dic1,f1)
#     json.dump(dic2,f1)
#     json.dump(dic3,f1)


# 读取数据
# with open(‘json文件1‘,encoding=‘utf-8‘) as f1:
#     print(json.load(f1))
    # print(json.load(f1))
    # print(json.load(f1))


# 正确写法:
dic1 = {username: alex}
dic2 = {username: 太白}
dic3 = {username: 大壮}
# with open(‘json文件1‘,encoding=‘utf-8‘,mode=‘w‘) as f1:
#     f1.write(json.dumps(dic1) + ‘\n‘)
#     f1.write(json.dumps(dic2) + ‘\n‘)
#     f1.write(json.dumps(dic3) + ‘\n‘)

# with open(‘json文件1‘,encoding=‘utf-8‘) as f1:
#     for i in f1:
#         print(json.loads(i))

 

Python基础-序列化 json,pickle

原文:https://www.cnblogs.com/qiuyubai/p/12803180.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!