关于json update的问题,希望能得到解答

场景再现:
已知文档a.txt内容如下:

{"list": [{"address": "http://www.baidu.com", "img": "http://www.baidu.com/logo.jpg", "title": "百度"}, {"address": "http://www.google.com", "img": "http://www.google.com/logo.jpg", "title": "谷歌"}, {"address": "http://www.yahoo.com", "img": "http://www.yahoo.com/logo.jpg", "title": "雅虎"}]}

b.txt内容如下

{"address": "http://www.sina.com", "img": "http://www.sina.com/logo.jpg", "title": "新浪"}
{"address": "http://www.qq.com", "img": "http://www.qq.com/logo.jpg", "title": "腾讯"}

请问如何通过json.update的命令把b.txt的内容添加到a.txt里面

Jason990420
最佳答案

json.update ? 方法 update 表示替换,而不是添加或追加 !

import json
import pprint

a_txt = """
{
  "list":
  [
    {"address": "http://www.baidu.com",  "img": "http://www.baidu.com/logo.jpg",  "title": "百度"},
    {"address": "http://www.google.com", "img": "http://www.google.com/logo.jpg", "title": "谷歌"},
    {"address": "http://www.yahoo.com", "img": "http://www.yahoo.com/logo.jpg",   "title": "雅虎"}
  ]
}
""".strip()

b_txt = """
{"address": "http://www.sina.com", "img": "http://www.sina.com/logo.jpg", "title": "新浪"}
{"address": "http://www.qq.com",   "img": "http://www.qq.com/logo.jpg",   "title": "腾讯"}
""".strip()

a = json.loads(a_txt)
b = list(map(eval, b_txt.split('\n')))

a["list"] += b

pprint.pprint(a)
{'list': [{'address': 'http://www.baidu.com',
           'img': 'http://www.baidu.com/logo.jpg',
           'title': '百度'},
          {'address': 'http://www.google.com',
           'img': 'http://www.google.com/logo.jpg',
           'title': '谷歌'},
          {'address': 'http://www.yahoo.com',
           'img': 'http://www.yahoo.com/logo.jpg',
           'title': '雅虎'},
          {'address': 'http://www.sina.com',
           'img': 'http://www.sina.com/logo.jpg',
           'title': '新浪'},
          {'address': 'http://www.qq.com',
           'img': 'http://www.qq.com/logo.jpg',
           'title': '腾讯'}]}
1年前 评论
讨论数量: 6
南城以南

你这个 不耻下问 就很灵魂

1年前 评论

这论坛,还要自甘卑贱才能答题吗

1年前 评论
chowjiawei

不耻下问原意是不把向学问、地位等不如自己的人请教当成可耻的事。形容谦虚、好学

1年前 评论
Jason990420

json.update ? 方法 update 表示替换,而不是添加或追加 !

import json
import pprint

a_txt = """
{
  "list":
  [
    {"address": "http://www.baidu.com",  "img": "http://www.baidu.com/logo.jpg",  "title": "百度"},
    {"address": "http://www.google.com", "img": "http://www.google.com/logo.jpg", "title": "谷歌"},
    {"address": "http://www.yahoo.com", "img": "http://www.yahoo.com/logo.jpg",   "title": "雅虎"}
  ]
}
""".strip()

b_txt = """
{"address": "http://www.sina.com", "img": "http://www.sina.com/logo.jpg", "title": "新浪"}
{"address": "http://www.qq.com",   "img": "http://www.qq.com/logo.jpg",   "title": "腾讯"}
""".strip()

a = json.loads(a_txt)
b = list(map(eval, b_txt.split('\n')))

a["list"] += b

pprint.pprint(a)
{'list': [{'address': 'http://www.baidu.com',
           'img': 'http://www.baidu.com/logo.jpg',
           'title': '百度'},
          {'address': 'http://www.google.com',
           'img': 'http://www.google.com/logo.jpg',
           'title': '谷歌'},
          {'address': 'http://www.yahoo.com',
           'img': 'http://www.yahoo.com/logo.jpg',
           'title': '雅虎'},
          {'address': 'http://www.sina.com',
           'img': 'http://www.sina.com/logo.jpg',
           'title': '新浪'},
          {'address': 'http://www.qq.com',
           'img': 'http://www.qq.com/logo.jpg',
           'title': '腾讯'}]}
1年前 评论

来个 shell 的:

jq -s '.[0].list += .[1:] | .[0]' a.txt b.txt

来个 Python 的:

import json

with open('a.txt', encoding='utf-8') as fp:
    a = json.load(fp)

with open('b.txt', encoding='utf-8') as fp:
    a['list'] += map(json.loads, filter(None, fp))

print(json.dumps(a, ensure_ascii=False, indent=2))

都输出:

{
  "list": [
    {
      "address": "http://www.baidu.com",
      "img": "http://www.baidu.com/logo.jpg",
      "title": "百度"
    },
    {
      "address": "http://www.google.com",
      "img": "http://www.google.com/logo.jpg",
      "title": "谷歌"
    },
    {
      "address": "http://www.yahoo.com",
      "img": "http://www.yahoo.com/logo.jpg",
      "title": "雅虎"
    },
    {
      "address": "http://www.sina.com",
      "img": "http://www.sina.com/logo.jpg",
      "title": "新浪"
    },
    {
      "address": "http://www.qq.com",
      "img": "http://www.qq.com/logo.jpg",
      "title": "腾讯"
    }
  ]
}
1年前 评论

感谢上面的两位大佬帮忙,这个论坛真的是非常的棒,在我学习python的路上总算不是孤军奋斗了

1年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!