【问题标题】:TypeError: 'xml.etree.ElementTree.Element' object is not callableTypeError:“xml.etree.ElementTree.Element”对象不可调用
【发布时间】:2017-09-09 00:29:59
【问题描述】:

我正在将我之前用 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(最近一次通话最后一次):文件 “/home/cali/PycharmProjects/Vocabulary/Vocabulary.py”,第 203 行,在 main() 文件“/home/cali/PycharmProjects/Vocabulary/Vocabulary.py”,第 198 行,在 主要的 gui = Vocabulary(root) 文件“/home/cali/PycharmProjects/Vocabulary/Vocabulary.py”,第 28 行,在 初始化 self.load_words() 文件“/home/cali/PycharmProjects/Vocabulary/Vocabulary.py”,第 168 行,在 load_words w = Word(node('Word').text, node('Explanation').text, node('Translation').text, node('Example').text) 类型错误: 'xml.etree.ElementTree.Element' 对象不可调用

这是原始的 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)

【问题讨论】:

    标签: python xml


    【解决方案1】:

    TypeError: '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'
    • 这意味着某些find(...) 没有找到参数中提到的名称的子元素,因此返回None,并且None 没有属性文本作为提到的错误消息。
    • 您问的如何解决?调试你的代码;找出在哪个node 上发生了异常以及为什么它没有您想要获取的子元素..
    • 示例。非常感谢您,先生。
    猜你喜欢
    • 1970-01-01
    • 2018-12-25
    • 2021-04-15
    • 2011-10-01
    • 2020-11-10
    • 2016-07-23
    • 2015-03-07
    • 2013-11-14
    相关资源
    最近更新 更多