2014-10-03 84 views
0

我想從一個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

回答

0

@ sk11給出了一些例子,我可以知道該怎麼做!這是簡單的比我在想,看:

for i,result in enumerate(result_set["results"]["bindings"]) : 
    print i, 
    for var in result_set["head"]["vars"] : 
     if var in result:#to test whether all variables exists or not 
      print result[var]["value"],": ", 
    print 

乾杯,

1

在Python中,你可以簡單地檢查開關是否在字典或

if key in dict 

不說了這麼多,這取決於什麼想在關鍵'gloss'未發現這樣做該字典您可以執行以下操作:

這將打印的所有其他鍵的值除'gloss'

for i,result in enumerate(result_set["results"]["bindings"]) : 
    print i, 
    for var in result_set["head"]["vars"]: 
     if var != 'gloss': 
      print result[var]["value"],"|--|", 
    print 

這只是打印我希望這是你想要的關鍵'gloss'

for i,result in enumerate(result_set["results"]["bindings"]) : 
    print i, 
    for var in result_set["head"]["vars"]: 
     if 'gloss' in result: # Checks whether you have gloss key in the result dict 
      print result[var]["value"],"|--|", 
    print 

值。

+0

感謝@ SK11!閱讀你的例子,我可以意識到我必須做的事情。其實,我只是想打印所有鍵的值,有或沒有'光澤'。 – Marcelo 2014-10-03 16:29:38

+0

所以現在你知道如何繼續。很高興幫助! – sk11 2014-10-03 18:53:14