gnucash-xea Datei mit ElementTree parsen?

Wenn du dir nicht sicher bist, in welchem der anderen Foren du die Frage stellen sollst, dann bist du hier im Forum für allgemeine Fragen sicher richtig.
Antworten
Benutzeravatar
jens
Python-Forum Veteran
Beiträge: 8502
Registriert: Dienstag 10. August 2004, 09:40
Wohnort: duisburg
Kontaktdaten:

Weiß jemand wie ich diese Datei: acctchrt_skr03.gnucash-xea mit ElementTree geparst bekomme?

Ich möchte die Daten der einzelnen SKR03 Konten haben.

Ein Auszug asu der Datei sieht so aus:

Code: Alles auswählen

...
  </gnc:account>
  <gnc:account version="2.0.0">
    <act:name>0027 EDV-Software</act:name>
    <act:id type="new">8275e7a0b489940a2ba993f9019a898f</act:id>
    <act:type>ASSET</act:type>
    <act:commodity>
      <cmdty:space>ISO4217</cmdty:space>
      <cmdty:id>EUR</cmdty:id>
    </act:commodity>
    <act:commodity-scu>100</act:commodity-scu>
    <act:code>0027</act:code>
    <act:slots>
      <slot>
	<slot:key>tax-related</slot:key>
	<slot:value type="integer">1</slot:value>
      </slot>
    </act:slots>
    <act:parent type="new">27356188a1963c1dbf943022441d095f</act:parent>
  </gnc:account>
  <gnc:account version="2.0.0">
...
Hab verschiedene Varianten mit et_root.findall("act") probiert. Mit "gnc:account" erhalte ich einen SyntaxError: expected path separator (:)

EDIT: Meine momentane Variante:

Code: Alles auswählen

    f = file(SKR_DATEI, "r")
    etree = ET.parse(f)
    f.close()
    root = etree.getroot()

    for node in root:
        if not node.tag.endswith("account"):
            continue
        print "_"*79
        data = {}
        for item in node:
            key = item.tag.split("}")[1]
            data[key] = item.text.strip()
        pprint(data)
Ausgabe (nur ein Eintrag) sieht so aus:

Code: Alles auswählen

{'code': '0027',
 'commodity': '',
 'commodity-scu': '100',
 'id': '8275e7a0b489940a2ba993f9019a898f',
 'name': '0027 EDV-Software',
 'parent': '27356188a1963c1dbf943022441d095f',
 'slots': '',
 'type': 'ASSET'}

GitHub | Open HUB | Xing | Linked in
Bitcoins to: 1JEgSQepxGjdprNedC9tXQWLpS424AL8cd
Leonidas
Python-Forum Veteran
Beiträge: 16025
Registriert: Freitag 20. Juni 2003, 16:30
Kontaktdaten:

Code: Alles auswählen

from xml.etree import ElementTree as ET
# nimmt übigens auch Dateipfade
root = ET.parse("acctchrt_skr03.gnucash-xea")
# Clark-Notation
print root.findall('{http://www.gnucash.org/XML/gnc}account')
# es wird immer mit Clark-Notation gearbeitet
#print [c for c in root.getiterator()]
My god, it's full of CARs! | Leonidasvoice vs (former) Modvoice
Benutzeravatar
jens
Python-Forum Veteran
Beiträge: 8502
Registriert: Dienstag 10. August 2004, 09:40
Wohnort: duisburg
Kontaktdaten:

Ah, danke. Hab bisher nix gemacht mit XML ;)

GitHub | Open HUB | Xing | Linked in
Bitcoins to: 1JEgSQepxGjdprNedC9tXQWLpS424AL8cd
Antworten