我想從一個SPARQL查詢,其給出了以下解釋數據類型打印結果:如何在Python中打印複雜的字典數據類型?
{u'head': {u'vars': [u'synonym', u'label', u'gloss']}, u'results': {u'bindings': [{u'synonym': {u'type': u'uri', u'value': u'http://arademaker.github.com/wn30-br/instances/wordsense-05256358-n-1'}, u'label': {u'type': u'literal', u'value': u'risco'}}, {u'synonym': {u'type': u'uri', u'value': u'http://arademaker.github.com/wn30-br/instances/wordsense-08593924-n-2'}, u'label': {u'type': u'literal', u'value': u'risco'}}, {u'synonym': {u'type': u'uri', u'value': u'http://arademaker.github.com/wn30-br/instances/wordsense-08593924-n-1'}, u'label': {u'type': u'literal', u'value': u'linha'}}, {u'synonym': {u'type': u'uri', u'value': u'http://arademaker.github.com/wn30-br/instances/wordsense-08593924-n-3'}, u'label': {u'type': u'literal', u'value': u'tra\xe7o'}}, {u'synonym': {u'type': u'uri', u'value': u'http://arademaker.github.com/wn30-br/instances/wordsense-04683136-n-1'}, u'label': {u'type': u'literal', u'value': u'risco'}}, {u'synonym': {u'type': u'uri', u'value': u'http://arademaker.github.com/wn30-br/instances/wordsense-04683136-n-2'}, u'label': {u'type': u'literal', u'value': u'listra'}}, {u'synonym': {u'type': u'uri', u'value': u'http://arademaker.github.com/wn30-br/instances/wordsense-14541852-n-3'}, u'label': {u'type': u'literal', u'value': u'perigo'}}, {u'synonym': {u'type': u'uri', u'value': u'http://arademaker.github.com/wn30-br/instances/wordsense-14541852-n-2'}, u'label': {u'type': u'literal', u'value': u'amea\xe7a'}}, {u'synonym': {u'type': u'uri', u'value': u'http://arademaker.github.com/wn30-br/instances/wordsense-14541852-n-1'}, u'label': {u'type': u'literal', u'value': u'risco'}}, {u'gloss': {u'type': u'literal', u'value': u'um risco empreendido sem levar em conta poss\xedveis perdas ou danos'}, u'synonym': {u'type': u'uri', u'value': u'http://arademaker.github.com/wn30-br/instances/wordsense-00802238-n-2'}, u'label': {u'type': u'literal', u'value': u'risco'}}, {u'gloss': {u'type': u'literal', u'value': u'um risco empreendido sem levar em conta poss\xedveis perdas ou danos'}, u'synonym': {u'type': u'uri', u'value': u'http://arademaker.github.com/wn30-br/instances/wordsense-00802238-n-1'}, u'label': {u'type': u'literal', u'value': u'perigo'}}, {u'synonym': {u'type': u'uri', u'value': u'http://arademaker.github.com/wn30-br/instances/wordsense-14541044-n-2'}, u'label': {u'type': u'literal', u'value': u'amea\xe7a'}}, {u'synonym': {u'type': u'uri', u'value': u'http://arademaker.github.com/wn30-br/instances/wordsense-14541044-n-1'}, u'label': {u'type': u'literal', u'value': u'risco'}}, {u'synonym': {u'type': u'uri', u'value': u'http://arademaker.github.com/wn30-br/instances/wordsense-14541044-n-3'}, u'label': {u'type': u'literal', u'value': u'perigo'}}]}}
我想打印出來,但我收到此錯誤信息:
0 http://arademaker.github.com/wn30-br/instances/wordsense-05256358-n-1 |--| risco |--|
Traceback (most recent call last):
File "/Users/marceloschiessl/RDF_text_project/tese/testDevelop.py", line 70, in <module>
print result[var]["value"],"|--|",
KeyError: u'gloss'
我知道我只有兩個記錄的變量u'gloss'的值。我想這是問題所在。
我有這樣的代碼來打印:
for i,result in enumerate(result_set["results"]["bindings"]) :
print i,
for var in result_set["head"]["vars"] :
print result[var]["value"],"|--|",
print
誰能幫助我?
TIA
感謝@ SK11!閱讀你的例子,我可以意識到我必須做的事情。其實,我只是想打印所有鍵的值,有或沒有'光澤'。 – Marcelo 2014-10-03 16:29:38
所以現在你知道如何繼續。很高興幫助! – sk11 2014-10-03 18:53:14