代码之家  ›  专栏  ›  技术社区  ›  James Black

append()函数在试图删除json文件中的重复项时不起作用-Python

  •  0
  • James Black  · 技术社区  · 1 年前

    我有一个json文件,它基于一个名为userid的列有一些重复项,我想使用append()函数删除这些重复项,这样它就可以输出一个与原始文件格式相同的新文件。

    这是一个json文件:

        [
            {
                "userid": "7126521576",
                "status": "UserStatus.OFFLINE",
                "name": "Avril Pauling",
                "bot": false,
                "username": "None"
            },
          {
                "userid": "7126521576",
                "status": "UserStatus.OFFLINE",
                "name": "Avril Pauling",
                "bot": false,
                "username": "None"
            },
            {
                "userid": "6571627119",
                "status": "UserStatus.OFFLINE",
                "name": "Laverne Alferez",
                "bot": false,
                "username": "None"
            },
            {
                "userid": "1995422560",
                "status": "UserStatus.OFFLINE",
                "name": "098767800",
                "bot": false,
                "username": "None"
            }
        ]
    

    删除重复的用户标识后的输出文件应为:

        [
            {
                "userid": "7126521576",
                "status": "UserStatus.OFFLINE",
                "name": "Avril Pauling",
                "bot": false,
                "username": "None"
            },
            {
                "userid": "6571627119",
                "status": "UserStatus.OFFLINE",
                "name": "Laverne Alferez",
                "bot": false,
                "username": "None"
            },
            {
                "userid": "1995422560",
                "status": "UserStatus.OFFLINE",
                "name": "098767800",
                "bot": false,
                "username": "None"
            }
        ]
    

    我已经尝试了以下代码,append()函数无法正常工作;它只附加最后一项:

        import json
        with open('target_user.json', 'r', encoding='utf-8') as f:
            jsons = json.load(f)
    
        jsons2 = []
        for item in jsons:
            if item['userid'] not in json2:
                jsons2.append(item)
                
        with open('target_user2.json', 'w', encoding='utf-8') as nf:
            json.dump(jsons2, nf, indent=4)
    

    非常感谢您的快速帮助。

    2 回复  |  直到 1 年前
        1
  •  0
  •   Tim Roberts    1 年前

    这应该可以满足您的需要:

    import json
    with open('target_user.json', 'r', encoding='utf-8') as f:
        jsons = json.load(f)
    
    ids = set()
    jsons2 = []
    for item in jsons:
        if item['userid'] not in ids:
            ids.add(item['userid'])
            jsons2.append(item)
            
    with open('target_user2.json', 'w', encoding='utf-8') as nf:
        json.dump(jsons2, nf, indent=4)
    
        2
  •  -1
  •   Imran S.    1 年前

    您在这里有一个拼写错误: jsons2 json2

    import json
    
    with open('target_user.json', 'r', encoding='utf-8') as f:
        jsons = json.load(f)
    
    jsons2 = []
    for item in jsons:
        if item['userid'] not in json2:
            jsons2.append(item)
            
    with open('target_user2.json', 'w', encoding='utf-8') as nf:
        json.dump(jsons2, nf, indent=4)
    

    您定义 jsons2 但是 if item['userid'] not in json2: 所以 json2 应该 jsons2 在这里

    请使用更正的代码重试。

    我没有足够的声誉发表评论,所以我回答了。