我想得到这个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"]
这段简单的代码遍历所有内容并查找所有引理值(顺便说一句,json应该用“而不是”作为字符串引号,我猜:
通过
sed -i 's/\'/\"/g' sample.json
转换为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
键始终存在:相关问题 更多 >
编程相关推荐