Seite 1 von 1

gnucash-xea Datei mit ElementTree parsen?

Verfasst: Freitag 1. August 2008, 15:35
von jens
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'}

Verfasst: Freitag 1. August 2008, 17:08
von Leonidas

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()]

Verfasst: Freitag 1. August 2008, 17:26
von jens
Ah, danke. Hab bisher nix gemacht mit XML ;)