python中json.dump()与json.dumps()的区别
1.将python数据结构转换为json字符串(json.dumps())
>>> import json >>> data={‘name‘:‘pipi‘,‘age‘:18} >>> json.dumps(data) ‘{"name": "pipi", "age": 18}‘
2.将json格式的字符串转换为python数据结构(json.loads())
>>> json_str=‘{"name": "pipi", "age": 18}‘ >>> json.loads(json_str) {‘name‘: ‘pipi‘, ‘age‘: 18}
3.将json对象写入文件中(json.dump())
data={‘name‘:‘pipi‘,‘age‘:18} >>> with open("c:/Users/cale/json.txt",‘w‘) as f: json.dump(data,f)
4.从文件中获取json对象
with open("c:/Users/cale/json.txt",‘r‘) as f: data=json.load(f) >>> data {‘name‘: ‘pipi‘, ‘age‘: 18} >>>
注:如果用f.read()获取的是一个字符串,而不是json对象
with open("c:/Users/cale/json.txt",‘r‘) as f: data=f.read() >>> data ‘{"name": "pipi", "age": 18}‘
相关推荐
夜斗不是神 2020-11-17
YENCSDN 2020-11-17
lsjweiyi 2020-11-17
houmenghu 2020-11-17
Erick 2020-11-17
HeyShHeyou 2020-11-17
以梦为马不负韶华 2020-10-20
lhtzbj 2020-11-17
pythonjw 2020-11-17
dingwun 2020-11-16
lhxxhl 2020-11-16
坚持是一种品质 2020-11-16
染血白衣 2020-11-16
huavhuahua 2020-11-20
meylovezn 2020-11-20
逍遥友 2020-11-20
weiiron 2020-11-16