类型错误: 'xml.etree.ElementTree.Element' 对象不可调用。

3
我正在将之前用C#编写的应用程序转换为Python。这是一个GUI应用程序,用于管理学习新语言时遇到的生词。当应用程序启动时,我必须从XML文件中加载单词,该文件具有相当简单的结构:
<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()方法:

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)
1个回答

2
错误类型:'xml.etree.ElementTree.Element' 对象不可调用。
正如错误信息所述,`node` 是一个 `Element`,而不是您可以像在此部分中所做的那样调用/调用的方法,即 `method_name(parameters)`。
w = Word(node('Word').text, node('Explanation').text, node('Translation').text, node('Example').text)

与 C# 中的 SelectSingleNode() 更接近的方法是 Element.find(),例如,要从 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)

我已将其更改为 w = Word(node.find('Word').text,node.find('Explanation').text,node.find('Translation').text,node.find('Example').text),但出现了“AttributeError:'NoneType' object has no attribute 'text'”的错误提示。 - user7822402
这意味着一些 find(...) 没有找到参数中提到的子元素,因此返回 None,并且 None 没有 text 属性,正如错误信息所述。 - har07
你问如何解决?调试你的代码;找出异常发生在哪个“节点”,以及为什么它没有你想要获取的子元素。 - har07
示例。非常感谢您,先生。 - user7822402

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接