美文网首页
爬虫 json数据类型转换

爬虫 json数据类型转换

作者: 年画儿 | 来源:发表于2019-08-06 16:14 被阅读0次

json有四个方法供我们进行数据转换:

mydict = {'name': 'xiaoming', 'age': 18}
#json.dumps 实现python类型转化为json字符串
json_str = json.dumps(mydict)   

#json.loads 实现json字符串转化为python的数据类型
my_dict = json.loads(json_str)

#json.dump 实现把python类型写入类文件对象
with open("temp.txt","w") as f:
    json.dump(mydict,f,ensure_ascii=False,indent=2)

# json.load 实现类文件对象中的json字符串转化为python类型
with open("temp.txt","r") as f:
    my_dict = json.load(f)

相关文章

网友评论

      本文标题:爬虫 json数据类型转换

      本文链接:https://www.haomeiwen.com/subject/kbhwdctx.html