标签:bsp 函数 open utf-8 文件 name nbsp count span
1 """ptyhon对象转json字符串""" 2 3 4 import json 5 6 persons = [ 7 {‘username‘: ‘张三‘, ‘age‘: 18, ‘country‘: ‘china‘}, 8 {‘username‘: ‘lisi‘, ‘age‘: 20, ‘country‘: ‘USA‘} 9 ] 10 # 使用dumps函数据把列表转成json字符串 11 json_str = json.dumps(persons) 12 print(type(json_str)) # <class ‘str‘> 13 print(json_str) 14 # 再把json字符串写入文件 15 with open(‘4_1person.json‘, ‘w‘) as fp: 16 fp.write(json_str) 17 18 # 也可直接把列表写入文件中 19 with open(‘4_1person2.json‘, ‘w‘) as fp: 20 json.dump(persons, fp) 21 # 对存在中文字符时不做处理会进行编码 22 with open(‘4_1person3.json‘, ‘w‘, encoding="UTF-8") as fp: 23 json.dump(persons, fp, ensure_ascii=False)
标签:bsp 函数 open utf-8 文件 name nbsp count span
原文地址:https://www.cnblogs.com/sruzzg/p/13138132.html