I want to get every value of 'Lemma' in this 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': []
}]
}
How can I get every value using python? There are five Lemma keys but I can't get all of them.
I've tried this, but it doesn't work:
for i in range(len(words)): #in this case the range of i would be 5
lemma = result["sentences"][0]["words"][i][1]["Lemma"]
I'm not sure why do you have this data structure - assuming you cannot change/reshape it to better suit your queries and use cases and that Lemma
key would always be present:
>>> [word[1]['Lemma']
for sentence in data['sentences']
for word in sentence['words']]
['Cinnamomum', '.', 'specific', 'immunoglobulin', 'measurement']
this simple code traverses everything and finds all Lemma values (btw. your json should have " instead of ' as string quotes, I guess:
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)
You can use the JSON encoder and decoder library
If you use that library you write:
import json json.loads(result)
Anyway, i try put your json in a validator and i obtain a error
change single quotes to double quotes by sed -i 's/\\'/\\"/g' sample.json
convert to json object and parse it by module 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'])
Result: Cinnamomum . specific immunoglobulin measurement
Cinnamomum . specific immunoglobulin measurement
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.