如何在Python中打印复杂的字典数据类型?

时间:2014-10-03 13:35:35

标签: python-2.7 dictionary printing

我想从sparql查询中打印一个结果,该查询提供以下字典数据类型:

{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&#39; gloss&#39;只有两个记录。我想这就是问题所在。

我有这个代码来打印它:

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

任何人都可以帮助我吗?

TIA

2 个答案:

答案 0 :(得分:1)

在Python中,您只需通过

检查密钥是否在字典中
if key in dict

话虽如此,根据在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

我希望这就是你想要的。

答案 1 :(得分: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

干杯,