标签:例子 操作 str 写入文件 list 字符串 load highlight bsp
例子1:
#json就是字符串 import json d = {"name":"abc"} json_str = json.dumps(d) #把字典/list转成字符串(json) print(json_str) json_str2 = ‘{"xmb":"123456","age":"30"}‘ dic = json.loads(json_str2) #把字符串(json)转成字典 print(dic)
例子2:
d1= { "id": 314, "name": "矿泉水", "sex": "男", "age": 18, "addr": "北京市昌平区", "grade": "摩羯座", "phone": "18317155663", "gold": 405 } with open(‘xmb.json‘,‘w‘,encoding=‘utf-8‘) as f: json.dump(d1,f,ensure_ascii=False,indent=8) #直接把字符串写入文件
例子3:
with open(‘xmb.json‘,encoding=‘utf-8‘) as f1: dic = json.load(f1) #直接读取文件内容,并转成字典格式 print(dic)
标签:例子 操作 str 写入文件 list 字符串 load highlight bsp
原文地址:https://www.cnblogs.com/xumb/p/11568413.html