json模块之dump

函数json.dump() 将json对象存储在扩展名为.json的文件中
json.dump(object, file)接受两个实参:存储的数据、存储数据的文件对象
可在json.dump()函数中添加indent属性,代表缩进字符个数,得到内容自动带缩进,格式更清晰
在json.dump()函数中添加ensure_ascii属性,规定编码格式
例:
url = ‘’ # 网址
resp = requests.get(url, params=param, headers=headers) # 网址请求
with open(“File3.json”, ‘w’, encoding=‘utf-8’) as f: # 创建并写入文件
json.dump(resp.json(), f, indent=2, ensure_ascii=False)

你可能感兴趣的:(python)