python 处理json

在这里插入图片描述

1、dumps:将python中的 字典 转换为 字符串

import json

test_dict = {'bigberg': [7600, {1: [['iPhone', 6300], ['Bike', 800], ['shirt', 300]]}]}
print(test_dict)
print(type(test_dict))
#dumps 将数据转换成字符串
json_str = json.dumps(test_dict)
print(json_str)
print(type(json_str))

2、loads: 将 字符串 转换为 字典

new_dict = json.loads(json_str)
print(new_dict)
print(type(new_dict))

3、dump: 将数据写入json文件中

with open("../config/record.json","w") as f:
     json.dump(new_dict,f)
     print("加载入文件完成...")

4、load:把文件打开,并把字符串变换为数据类型

with open("../config/record.json",'r') as load_f:
    load_dict = json.load(load_f)
    print(load_dict)
load_dict['smallberg'] = [8200,{1:[['Python',81],['shirt',300]]}]
print(load_dict)

with open("../config/record.json","w") as dump_f:
    json.dump(load_dict,dump_f)

5、遍历key value


test_json = {“a”:1,“b”:2,“c”:3}

for key,value in test_json.items():

print key,value
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值