2017-03-16 44 views
0

我想從文件中的json數組中提取特定鍵的值。我試圖從key中取出值,這些key在json數組中的任何地方都可用。下面是我使用的代碼:JSON解析與Python3.6給出問題

from xml.dom.minidom import parseString 
import json   
def bar(somejson, key): 
    def val(node): 
     # Searches for the next Element Node containing Value 
     e = node.nextSibling 
     while e and e.nodeType != e.ELEMENT_NODE: 
      e = e.nextSibling 
     return (e.getElementsByTagName('string')[0].firstChild.nodeValue if e 
       else None) 
    # parse the JSON as XML 
    foo_dom = parseString(xmlrpclib.dumps((json.loads(somejson),))) 
    # and then search all the name tags which are P1's 
    # and use the val user function to get the value 
    return [val(node) for node in foo_dom.getElementsByTagName('name') 
      if node.firstChild.nodeValue in key] 

這我已經從這個問題了:How can I use python finding particular json value by key?
現在我在xmlrpclib得到任何錯誤。我不明白爲什麼它會到來。我嘗試使用pip獲得軟件包,但沒有可用於安裝的軟件包。
請問我該怎麼辦?數據示例與上述問題鏈接中的示例相同。

+0

您是否正在導入'xmlrpclib'庫?順便說一句,這個庫可以在['PyPi'](https://pypi.python.org/pypi/xmlrpclib) –

+0

@MoinuddinQuadri如何安裝它?我試圖設置,但不能或有沒有辦法在我的程序中使用它。請幫忙。 –

+0

@JafferWilson你看到我的回答嗎?它仍然是標準庫的一部分。 – Vallentin

回答

0

在Python 3中,xmlrpclib庫被重命名爲xmlrpcdumps現在位於xmlrpc.client。因此,要解決您的問題要麼是:

import xmlrpc.client as xmlrpclib 

xmlrpc.client.dumpsimport xmlrpc.client更換xmlrpclib.dumps

+0

謝謝,這有助於很多.. :) –