标签:test python 文件 介绍 int open post arch with open
1.json.dumps()用于将dict类型的数据转成str
import json
dict = {‘a‘: ‘wo‘, ‘b‘: ‘zai‘, ‘c‘: ‘zhe‘, ‘d‘: ‘li‘}
string = json.dumps(dict)
print(dict)
print(string)
print(type(dict))
print(type(string))
with open(r‘C:\Users\zy\Documents\GitHub\python3\searchTest\json.json‘,‘w‘)as f:
f.write(string)
2.json.dump()用于将dict类型的数据转成str,并写入到json文件中
dict = {‘a‘: ‘wo‘, ‘b‘: ‘zai‘, ‘c‘: ‘zhe‘, ‘d‘: ‘li‘}
json.dump(dict,open(r‘C:\Users\zy\Documents\GitHub\python3\searchTest\json.json‘,‘w‘))
3.json.loads():用于将str类型的数据转成dict
dict = {‘a‘: ‘wo‘, ‘b‘: ‘zai‘, ‘c‘: ‘zhe‘, ‘d‘: ‘li‘}
dumps = json.dumps(dict)
loads = json.loads(dumps)
print(dict)
print(dumps)
print(loads)
print(type(dict))
print(type(dumps))
print(type(loads))
4.json.load()用于从json文件中读取数据
filename = (r‘C:\Users\zy\Documents\GitHub\python3\searchTest\json.json‘)
jsObj = json.load(open(filename))
print(jsObj)
print(type(jsObj))
python:Json模块dumps、loads、dump、load介绍
标签:test python 文件 介绍 int open post arch with open
原文地址:https://www.cnblogs.com/WebLinuxStudy/p/11800328.html