首页 > 编程语言 > 详细

python 读写json

时间:2018-09-03 11:39:43      阅读:101      评论:0      收藏:0      [点我收藏+]

python 读写json

  1. 将python dict 写入json文件
    • json.dump()

    •   import json
        dict = {‘a‘:1213,‘b‘:[‘ad‘,3,‘23fs‘]}     
        with open("file_path", "w") as json_f:
            json.dump(dict, json_f)
  2. 读取json文件
    • json.load()
    •   import json()
        with open("file_path", "r") as json_f:
            dict = json.load(json_f)
  3. 将python字典转换成json字符串
    • json.dumps()
    •   import json
        dict = {‘a‘:1213,‘b‘:[‘ad‘,3,‘23fs‘]} 
        dict_str = json.dumps(dict)
  4. 将json字符串转换成python字典
    • json.loads()
    •   import json
        dict_str = ‘{‘a‘:1213,‘b‘:[‘ad‘,3,‘23fs‘]}‘ 
        dict = json.loads(dict_str)

python 读写json

原文:https://www.cnblogs.com/liushi-Oscar/p/9577529.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!