python获取json上的特定值

2024-10-04 11:29:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我想得到这个json中“引理”的每个值:

{'sentences': 
   [{'indexeddependencies': [], 'words': 
     [
        ['Cinnamomum', {'CharacterOffsetBegin': '0', 'CharacterOffsetEnd': '10', 'Lemma': 'Cinnamomum', 'PartOfSpeech': 'NNP', 'NamedEntityTag': 'O'}], 
        ['.', {'CharacterOffsetBegin': '14', 'CharacterOffsetEnd': '15', 'Lemma': '.', 'PartOfSpeech': '.', 'NamedEntityTag': 'O'}]
     ], 'parsetree': [], 'text': 'Cinnamomum.', 'dependencies': []
    }, 
    {'indexeddependencies': [], 'words': 
      [
        ['specific', {'CharacterOffsetBegin': '16', 'CharacterOffsetEnd': '24', 'Lemma': 'specific', 'PartOfSpeech': 'JJ', 'NamedEntityTag': 'O'}],
        ['immunoglobulin', {'CharacterOffsetBegin': '25', 'CharacterOffsetEnd': '39', 'Lemma': 'immunoglobulin', 'PartOfSpeech': 'NN', 'NamedEntityTag': 'O'}],
        ['measurement', {'CharacterOffsetBegin': '51', 'CharacterOffsetEnd': '62', 'Lemma': 'measurement', 'PartOfSpeech': 'NN', 'NamedEntityTag': 'O'}]
      ], 'parsetree': [], 'text': 'specific immunoglobulin measurement', 'dependencies': []
     }]
}

如何使用python获得每个值?有五个引理键,但我不能全部得到。你知道吗

我试过了,但没用:

for i in range(len(words)): #in this case the range of i would be 5
      lemma = result["sentences"][0]["words"][i][1]["Lemma"]

Tags: textsentencesdependenciesmeasurementwordsspecificlemmapartofspeech
3条回答

这段简单的代码遍历所有内容并查找所有引理值(顺便说一句,json应该用“而不是”作为字符串引号,我猜:

import json

with open('lemma.json') as f:
    data = json.load(f)


def traverse(node):
    for key in node:
        if isinstance(node, list):
            traverse(key)
        elif isinstance(node, dict):
            if key == 'Lemma':
                print key, node[key]
                continue
            traverse(node[key])

traverse(data)
  1. 通过sed -i 's/\'/\"/g' sample.json

  2. 转换为json对象并通过模块json解析 import json with open('sample.json', encoding='utf-8') as data_file: data = json.loads(data_file.read()) for sentence in data['sentences']: for word in sentence['words']: print(word[1]['Lemma'])

结果: Cinnamomum . specific immunoglobulin measurement

我不知道为什么会有这样的数据结构—假设您无法更改/重塑它以更好地适应查询和用例,并且Lemma键始终存在:

>>> [word[1]['Lemma'] 
     for sentence in data['sentences'] 
     for word in sentence['words']]
['Cinnamomum', '.', 'specific', 'immunoglobulin', 'measurement']

相关问题 更多 >