美文网首页
JSON文件与字典

JSON文件与字典

作者: 微雨旧时歌丶 | 来源:发表于2019-03-06 10:45 被阅读0次

https://blog.csdn.net/qq_23926575/article/details/76566209

  • 保存字典为json文件
dic = {"商家名称": "井格老灶火锅(望京新世界店)", "评分": 26.2, "地址": "火锅望京广顺南大街路16号", "人均消费": 105, "评论数量": 1387}
with codecs.open('hg.json','a', 'utf-8') as outf:
    json.dump(dic, outf, ensure_ascii=False)
    outfile.write('\n')
  • 读取json文件为字典
# -*- coding: utf-8 -*-
import json
import codecs

data = []
with codecs.open("hg.json", "r", "utf-8") as f:
    for line in f:
        dic = json.loads(line)
        data.append(dic)
        print json.dumps(dic, indent=4, ensure_ascii=False, encoding='utf-8')

相关文章

网友评论

      本文标题:JSON文件与字典

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