2017-04-13 30 views
2

私は以前にC#で書いたアプリケーションをPythonに変換しています。これは、新しい言語を学んでいる間に未知語を管理するGUIアプリケーションです。TypeError: 'xml.etree.ElementTree.Element'オブジェクトは呼び出し可能ではありません

<Words> 
    <Word> 
     <Word>test</Word> 
     <Explanation>test</Explanation> 
     <Translation>test</Translation> 
     <Examples>test</Examples> 
    </Word> 
</Words> 

それにもかかわらず、私は取得しています:

/usr/bin/python3.5 /home/cali/PycharmProjects/Vocabulary/Vocabulary.py Traceback (most recent call last): File "/home/cali/PycharmProjects/Vocabulary/Vocabulary.py", line 203, in main() File "/home/cali/PycharmProjects/Vocabulary/Vocabulary.py", line 198, in main gui = Vocabulary(root) File "/home/cali/PycharmProjects/Vocabulary/Vocabulary.py", line 28, in init self.load_words() File "/home/cali/PycharmProjects/Vocabulary/Vocabulary.py", line 168, in load_words w = Word(node('Word').text, node('Explanation').text, node('Translation').text, node('Example').text) TypeError: 'xml.etree.ElementTree.Element' object is not callable

これは、元LoadWords(ある)メソッドをアプリケーションが起動すると、私は非常に単純な構造を持つXMLファイルから単語をロードする必要がある場合:

void LoadWords() 
{ 
    words.Clear(); 
    listView1.Items.Clear(); 
    string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); 
    string vocabulary_path = path + "\\Vocabulary\\Words.xml"; 
    if (!Directory.Exists(path + "\\Vocabulary")) 
     Directory.CreateDirectory(path + "\\Vocabulary"); 

    if (!File.Exists(vocabulary_path)) 
    { 
     XmlTextWriter xW = new XmlTextWriter(vocabulary_path, Encoding.UTF8); 
     xW.WriteStartElement("Words"); 
     xW.WriteEndElement(); 
     xW.Close(); 
    } 
    XmlDocument xDoc = new XmlDocument(); 
    xDoc.Load(vocabulary_path); 
    foreach (XmlNode xNode in xDoc.SelectNodes("Words/Word")) 
    { 
     Word w = new Word(); 
     w.WordOrPhrase = xNode.SelectSingleNode("Word").InnerText; 
     w.Explanation = xNode.SelectSingleNode("Explanation").InnerText; 
     w.Translation = xNode.SelectSingleNode("Translation").InnerText; 
     w.Examples = xNode.SelectSingleNode("Examples").InnerText; 
     words.Add(w); 
     listView1.Items.Add(w.WordOrPhrase); 
     WordCount(); 
    } 

} 

各ノードの内部テキストへのアクセス方法はわかりません。ここで

は私のload_words機能である:前述のエラーメッセージとして

def load_words(self): 

    self.listBox.delete(0, END) 
    self.words.clear() 

    path = os.path.expanduser('~/Desktop') 
    vocabulary = os.path.join(path, 'Vocabulary', 'Words.xml') 

    if not os.path.exists(vocabulary): 
     if not os.path.exists(os.path.dirname(vocabulary)): 
      os.mkdir(os.path.dirname(vocabulary)) 
     doc = ET.Element('Words') 
     tree = ET.ElementTree(doc) 
     tree.write(vocabulary) 
    else: 
     tree = ET.ElementTree(file=vocabulary) 

    for node in tree.findall('Word'): 
     w = Word(node('Word').text, node('Explanation').text, node('Translation').text, node('Example').text) 

     self.words.append(w) 
     self.listBox.insert(w.wordorphrase) 

答えて

0

TypeError: 'xml.etree.ElementTree.Element' object is not callable

nodeはあなたがこの部分で行ったように/ method_name(parameters)のように呼び出す呼び出すことができない方法Element、次のとおりです。

w = Word(node('Word').text, node('Explanation').text, node('Translation').text, node('Example').text) 

あなたのC#でSelectSingleNode()に近いメソッドは、たとえば、f32sを得るためにElement.find()になりますその後、T子nodeからWordという要素と内側のテキスト抽出:

inner_text = node.find('Word').text 

を、次のように自分の状況コードで実装は次のようになります。

w = Word(node.find('Word').text, node.find('Explanation').text, node.find('Translation').text, node.find('Example').text) 
+0

私は 'W =ワード(ノードにそれを変更しましたテキスト、node.find( '例')。テキスト) '、しかし、AttributeErrorを取得しています。 : 'NoneType'オブジェクトに 'text'という属性がありません –

+0

つまり、 'find(...)'の中には、パラメータに指定された名前の子要素が見つからず、 'None'と' None'属性テキストはtとしてありません彼は言ったエラーメッセージ。 – har07

+0

あなたはそれを修正する方法を尋ねた?コードをデバッグします。どのノードで例外が発生したのか、なぜそれが得たい子要素を持っていないのかを調べてください。 – har07

関連する問題