python - 如何使用 python 消除 json 文件中的冗余

标签 python json

我有一个json文件,如下

{
  "question": "yellow skin around wound from cat bite. why?",
  "answer": "this may be the secondary result of a resolving bruise but a cat bite is a potentially serious and complicated wound and should be under the care of a physician.",
  "tags": [
    "wound care"
  ]
},
{
  "question": "yellow skin around wound from cat bite. why?",
  "answer": "see your doctor with all deliberate speed. or go to an urgent care center or a hospital emergency room. do it fast!",
  "tags": [
    "wound care"
  ]
},

正如您所看到的,冗余部分仅位于按键的“问题”部分,但答案各不相同,这意味着该数据是从论坛中提取的,并且它包含不同的答案同样的问题,有没有办法使用 pyton 消除冗余部分或将答案分组在一起。 谢谢

最佳答案

需要某种分组。有很多方法可以做到这一点,包括来自 itertools 模块的函数、pandas 等外部模块以及其他来源。这是使用内置结构 defaultdict 的一种方法:

from collections import defaultdict
import json

data = json.loads(rawdata)
questions = defaultdict(list)
for row in data:
    question = row.pop('question')
    questions[question].append(row)

结果将是一个以问题为键的字典questions(准确地说是defaultdict),其值给出了结果答案和标签。一个缺点是这会破坏性地改变您原始解析的 JSON 数据。您可以通过多种方式来解决这个问题,为简洁起见,我将省略这些方法。

这是生成的 questions 字典的简化版本:

{'yellow skin ...why?': [{'answer': 'this may be the secondary result of a '
                                    'resolving bruise but a cat bite is a '
                                    'potentially serious and complicated wound '
                                    'and should be under the care of a '
                                    'physician.',
                          'tags': ['wound care']},
                         {'answer': 'see your doctor with all deliberate '
                                    'speed. or go to an urgent care center or '
                                    'a hospital emergency room. do it fast!',
                          'tags': ['wound care']}]}

关于python - 如何使用 python 消除 json 文件中的冗余,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54789719/

相关文章:

python - 如何使用 PyQT 对话框标题栏上的 "?"(这个小部件是什么)

python - BeautifulSoup : Extracting all the <br/> from the <strong>

javascript - 服务器生成的具有长帖子内容的 PDF angularjs

javascript - 我的 Node js 项目中的全局变量出现问题

json - 如何将变量传递到转义的json字符串中?

python - 使用 Pandas 引用下一行值

python - 如何在 virtualenv 的子目录中使用 pipelinev 创建新的 virtualenv?

Python:如何定义自定义分布?

javascript - 对象数组返回与特定 ID 关联的标签

json - 将原始 JSON 加载到 Pig 中