문제

I'm trying to get my head around the dbpedia JSON schema and can't figure out an efficient way of extracting a specific node:

This is what dbpedia gives me:

http://dbpedia.org/data/Ceramic_art.json

I've got the whole thing as a JSON object in Python but don't really understand how to get the english abstract from this data. I've gotten this far:

u = "http://dbpedia.org/data/Ceramic_art.json"
data = urlfetch.fetch(url=u)
json_data = json.loads(data.content)

for j in json_data["http://dbpedia.org/resource/Ceramic_art"]:
    if(j == "http://dbpedia.org/ontology/abstract"):
        print "it's here"

Not sure how to proceed from here. As you can see there are multiple languages. I need to get the english abstract.

Thanks for your help,

g

도움이 되었습니까?

해결책


print [abstract['value'] for abstract in json_data["http://dbpedia.org/resource/Ceramic_art"]["http://dbpedia.org/ontology/abstract"] if abstract['lang'] == 'en'][0]

Obviously, you'd want to do more error checking than that, in case the data is bad, but that's the basic idea.

다른 팁

It's a list of dicts. Just iterate through the elements of the list until you find the one whose value for u'lang' is u'en'.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top