无法使用Python

时间:2019-01-03 07:40:59

标签: python json

我有一个XML格式的输出,并且已经将该XML格式的输出转换为JSON。将数据转换为JSON之后,我只需要打印选定的输出。

api = NaElement("snapmirror-get")


xo = s.invoke_elem(api)
if (xo.results_status() == "failed"):
    print ("Error:\n")
    print (xo.sprintf())
    sys.exit(1)

print ("Received:\n")
print (xo.sprintf())
data = json.loads(json.dumps(xmltodict.parse(xo.sprintf())))

这是我用来将XML文件转换为JSON的代码。输出将保存在data变量中,我只需要打印选定的输出。下面是数据变量的输出。

  

{'results':{'@status':'passed','attributes':{'snapmirror-info':   {'break-failed-count':'0','break-successful-count':'0',   '目标位置':'svm-svm1-vault:volume1',   'destination-volume':'volume1','destination-volume-node':   'svm1cdot1-02','destination-vserver':'svm-svm1-vault',   'destination-vserver-uuid':'545fa72b-e84c-11e6-ae0a-00a098546e58',   'exported-snapshot':'daily.2019-01-03_0010',   'exported-snapshot-timestamp':'1546454401','is-constituent':   'false','is-healthy':'true','lag-time':'46182',   'last-transfer-duration':'759','last-transfer-end-stamp':   '1546469259','最后传送自':'svm-svm1:volume1',   'last-transfer-network-compression-ratio':'1:1',   'last-transfer-size':'5192737823','last-transfer-type':'update',   'max-transfer-rate':'0','mirror-state':'snapmirrored',   'news-snapshot':'daily.2019-01-03_0010',   '最新快照时间戳':'1546454401','opmask':   '18446744073709551615','policy':'Daily_policy1','policy-type':   'vault','relationship-control-plane':'v2',   'relationship-group-type':'none','relationship-id':   '4557665a-ebcd-11e6-ae0a-00a098546e58','relationship-status':'idle',   'relationship-type':'vault','resync-failed-count':'0',   'resync-successful-count':'0','schedule':Rep_Set_01',   'source-location':''svm-svm1:volume1','source-volume':'volume1',   '源-vserver':'svm-scr1','源-vserver-uuid':   'acce074e-e14d-11e6-93ae-00a09899b4be','总传输字节':   '27879000066','总传输时间-秒':'2043',   'update-failed-count':'0','update-successful-count':'8','vserver':   'svm-svm1-vault'}}}}

从上面的输出中,我只需要打印lag-time值和source-volume名称输出。

1 个答案:

答案 0 :(得分:1)

您的输出中有错字(我试图编辑您的问题,但还有另一个编辑待处理。)

使用JSON访问数据非常简单。以您的示例为例:

output = {'results': {'@status': 'passed', 'attributes': {'snapmirror-info': {'break-failed-count': '0', 'break-successful-count': '0', 'destination-location': 'svm-svm1-vault:volume1', 'destination-volume': 'volume1', 'destination-volume-node': 'svm1cdot1-02', 'destination-vserver': 'svm-svm1-vault', 'destination-vserver-uuid': '545fa72b-e84c-11e6-ae0a-00a098546e58', 'exported-snapshot': 'daily.2019-01-03_0010', 'exported-snapshot-timestamp': '1546454401', 'is-constituent': 'false', 'is-healthy': 'true', 'lag-time': '46182', 'last-transfer-duration': '759', 'last-transfer-end-timestamp': '1546469259', 'last-transfer-from': 'svm-svm1:volume1', 'last-transfer-network-compression-ratio': '1:1', 'last-transfer-size': '5192737823', 'last-transfer-type': 'update', 'max-transfer-rate': '0', 'mirror-state': 'snapmirrored', 'newest-snapshot': 'daily.2019-01-03_0010', 'newest-snapshot-timestamp': '1546454401', 'opmask': '18446744073709551615', 'policy': 'Daily_policy1', 'policy-type': 'vault', 'relationship-control-plane': 'v2', 'relationship-group-type': 'none', 'relationship-id': '4557665a-ebcd-11e6-ae0a-00a098546e58', 'relationship-status': 'idle', 'relationship-type': 'vault', 'resync-failed-count': '0', 'resync-successful-count': '0', 'schedule': 'Rep_Set_01', 'source-location': 'svm-svm1:volume1', 'source-volume': 'volume1', 'source-vserver': 'svm-scr1', 'source-vserver-uuid': 'acce074e-e14d-11e6-93ae-00a09899b4be', 'total-transfer-bytes': '27879000066', 'total-transfer-time-secs': '2043', 'update-failed-count': '0', 'update-successful-count': '8', 'vserver': 'svm-svm1-vault'}}}}

enc = json.dumps(output)
dec = json.loads(enc)

print(dec['results']['attributes']['snapmirror-info']['lag-time'])
print(dec['results']['attributes']['snapmirror-info']['source-volume'])

>46182
>volume1

如果要遍历它们,可以执行以下操作:

rel = dec["results"]["attributes"]["snapmirror-info"]
for k in rel:
      # if k in ['lag-time', 'source-volume'] :
    print(k, rel[k])