Ich konvertiere zu Python eine Anwendung, die ich zuvor in C# geschrieben hatte. Es ist eine GUI-Anwendung, um unbekannte Wörter zu verwalten und gleichzeitig eine neue Sprache zu lernen. Wenn die Anwendung gestartet wird, muss ich die Worte aus der XML-Datei laden, die eine ziemlich einfache Struktur hat:TypeError: 'xml.etree.ElementTree.Element' -Objekt ist nicht aufrufbar
<Words>
<Word>
<Word>test</Word>
<Explanation>test</Explanation>
<Translation>test</Translation>
<Examples>test</Examples>
</Word>
</Words>
Trotzdem bin ich immer:
/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
Das sind die ursprünglichen LoadWords ist() -Methode :
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();
}
}
Ich weiß nicht, wie auf den inneren Text jedes Knotens zugegriffen wird.
Hier ist meine load_words Funktion:
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)
Ich habe es geändert 'w = Wort (Knoten .find ('Word'). text, node.find ('Erklärung'). text, node.find ('Übersetzung'). text, node.find ('Beispiel'). text) ', bekomme aber' AttributeError : 'NoneType' -Objekt hat kein Attribut 'text'' –
Das bedeutet, dass einige der 'find (...)' das untergeordnete Element mit dem im Parameter erwähnten Namen nicht gefunden haben und daher 'None' und' None' zurückgeben hat keinen Attributtext als t Die Fehlermeldung wurde erwähnt. – har07
Wie es zu beheben Sie gefragt? Debuggen Sie Ihren Code; Finde heraus, an welchem "Knoten" die Ausnahme aufgetreten ist und warum sie nicht das Kindelement hatte, das du bekommen willst. – har07