Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.

Load words from XML file

[es] :: Python :: Load words from XML file

[ Pregleda: 8489 | Odgovora: 1 ] > FB > Twit

Postavi temu Odgovori

Autor

Pretraga teme: Traži
Markiranje Štampanje RSS

wraith46

Član broj: 336363
Poruke: 29



+1 Profil

icon Load words from XML file12.04.2017. u 19:32 - pre 84 meseci
Imam jednu metodu napisanu u C#:

Code:
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();
    }

}


...koju treba da konvertujem u Python. Evo sta sam dosad uradio:

Code:

    def load_words(self):

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

        vocabulary = os.path.join(path, "Vocabulary", "Words.xml")

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

wraith46

Član broj: 336363
Poruke: 29



+1 Profil

icon Re: Load words from XML file13.04.2017. u 15:27 - pre 84 meseci
Finalno resenje:

Code:
   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.find('Word').text, node.find('Explanation').text, node.find('Translation').text,
                     node.find('Examples').text)

            self.words.append(w)
            self.listBox.insert(END, w.wordorphrase)
 
Odgovor na temu

[es] :: Python :: Load words from XML file

[ Pregleda: 8489 | Odgovora: 1 ] > FB > Twit

Postavi temu Odgovori

Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.