刚学习,不知道如何解决关于文件替换的问题,望大佬告知

有两个文件,分别为文件a.txt和b.txt(json格式)

a.txt里面内容

http://www.aaa.com
http://www.bbb.com
http://www.ccc.com

b.txt里面内容

{
  "pindao": [
    {
      "address": "http://www.aaa.com",
      "img": "http://cdn.youlxodxka.live/img/1.jpg",
      "title": "CCTV1综合"
    },
    {
      "address": "http://117.148.179.156/PLTV/88888888/224/3221232362/index.m3u8",
      "img": "http://cdn.youlxodxka.live/img/2.jpg",
      "title": "CCTV2财经"
    },
    {
      "address": "http://shbu.live.bestvcdn.com.cn:8080/live/program/live/cctv3hd/2300000/mnf.m3u8",
      "img": "http://cdn.youlxodxka.live/img/3.jpg",
      "title": "cctv3综艺"
    },
    {
      "address": "http://www.bbb.com",
      "img": "http://cdn.youlxodxka.live/img/4.jpg",
      "title": "CCTV4国际"
    },
    {
      "address": "http://117.148.179.165/PLTV/88888888/224/3221231702/index.m3u8",
      "img": "http://cdn.youlxodxka.live/img/5.jpg",
      "title": "CCTV5体育"
    },
    {
      "address": "http://www.ccc.com",
      "img": "http://cdn.youlxodxka.live/img/5p.jpg",
      "title": "CCTV5+体育"
    },
    {
      "address": "http://39.134.65.175/PLTV/88888888/224/3221225814/index.m3u8",
      "img": "http://cdn.youlxodxka.live/img/6.jpg",
      "title": "CCTV6电影"
    }
  ]
}

想要实现的效果:
如果b.txt的address值里面出现a.txt里的预设值,然后整行清空,只保留没有对比上的数据,效果如下,请问怎么写这个规则:

{
  "pindao": [
    {
      "address": "http://117.148.179.156/PLTV/88888888/224/3221232362/index.m3u8",
      "img": "http://cdn.youlxodxka.live/img/2.jpg",
      "title": "CCTV2财经"
    },
    {
      "address": "http://shbu.live.bestvcdn.com.cn:8080/live/program/live/cctv3hd/2300000/mnf.m3u8",
      "img": "http://cdn.youlxodxka.live/img/3.jpg",
      "title": "cctv3综艺"
    },
    {
      "address": "http://117.148.179.165/PLTV/88888888/224/3221231702/index.m3u8",
      "img": "http://cdn.youlxodxka.live/img/5.jpg",
      "title": "CCTV5体育"
    },
    {
      "address": "http://39.134.65.175/PLTV/88888888/224/3221225814/index.m3u8",
      "img": "http://cdn.youlxodxka.live/img/6.jpg",
      "title": "CCTV6电影"
    }
  ]
}

万分感谢,各位大佬可以给我指一个方向,或者是有代码给我学习一下就更好了

Jason990420
最佳答案

Demo code

import json
import pprint

a_txt = """
http://www.aaa.com
http://www.bbb.com
http://www.ccc.com
""".strip()

b_txt = """
{
  "pindao": [
    {
      "address": "http://www.aaa.com",
      "img": "http://cdn.youlxodxka.live/img/1.jpg",
      "title": "CCTV1综合"
    },
    {
      "address": "http://117.148.179.156/PLTV/88888888/224/3221232362/index.m3u8",
      "img": "http://cdn.youlxodxka.live/img/2.jpg",
      "title": "CCTV2财经"
    },
    {
      "address": "http://shbu.live.bestvcdn.com.cn:8080/live/program/live/cctv3hd/2300000/mnf.m3u8",
      "img": "http://cdn.youlxodxka.live/img/3.jpg",
      "title": "cctv3综艺"
    },
    {
      "address": "http://www.bbb.com",
      "img": "http://cdn.youlxodxka.live/img/4.jpg",
      "title": "CCTV4国际"
    },
    {
      "address": "http://117.148.179.165/PLTV/88888888/224/3221231702/index.m3u8",
      "img": "http://cdn.youlxodxka.live/img/5.jpg",
      "title": "CCTV5体育"
    },
    {
      "address": "http://www.ccc.com",
      "img": "http://cdn.youlxodxka.live/img/5p.jpg",
      "title": "CCTV5+体育"
    },
    {
      "address": "http://39.134.65.175/PLTV/88888888/224/3221225814/index.m3u8",
      "img": "http://cdn.youlxodxka.live/img/6.jpg",
      "title": "CCTV6电影"
    }
  ]
}
""".strip()

urls = a_txt.split('\n')
lst = json.loads(b_txt)["pindao"]
result = {'pindao':[item for item in lst if item['address'] not in urls]}
pprint.pprint(result)
2周前 评论
讨论数量: 2
Jason990420

Demo code

import json
import pprint

a_txt = """
http://www.aaa.com
http://www.bbb.com
http://www.ccc.com
""".strip()

b_txt = """
{
  "pindao": [
    {
      "address": "http://www.aaa.com",
      "img": "http://cdn.youlxodxka.live/img/1.jpg",
      "title": "CCTV1综合"
    },
    {
      "address": "http://117.148.179.156/PLTV/88888888/224/3221232362/index.m3u8",
      "img": "http://cdn.youlxodxka.live/img/2.jpg",
      "title": "CCTV2财经"
    },
    {
      "address": "http://shbu.live.bestvcdn.com.cn:8080/live/program/live/cctv3hd/2300000/mnf.m3u8",
      "img": "http://cdn.youlxodxka.live/img/3.jpg",
      "title": "cctv3综艺"
    },
    {
      "address": "http://www.bbb.com",
      "img": "http://cdn.youlxodxka.live/img/4.jpg",
      "title": "CCTV4国际"
    },
    {
      "address": "http://117.148.179.165/PLTV/88888888/224/3221231702/index.m3u8",
      "img": "http://cdn.youlxodxka.live/img/5.jpg",
      "title": "CCTV5体育"
    },
    {
      "address": "http://www.ccc.com",
      "img": "http://cdn.youlxodxka.live/img/5p.jpg",
      "title": "CCTV5+体育"
    },
    {
      "address": "http://39.134.65.175/PLTV/88888888/224/3221225814/index.m3u8",
      "img": "http://cdn.youlxodxka.live/img/6.jpg",
      "title": "CCTV6电影"
    }
  ]
}
""".strip()

urls = a_txt.split('\n')
lst = json.loads(b_txt)["pindao"]
result = {'pindao':[item for item in lst if item['address'] not in urls]}
pprint.pprint(result)
2周前 评论

感谢大佬,看了以后才发现很简单,可能我们这种新手缺的就是有人指点一下,万分感谢

2周前 评论

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