XML-Datei ein- und auslesen / UTF-8
Verfasst: Montag 8. Februar 2021, 09:26
Hallo,
ich habe zwei Funktionen zur Umwandlung einer kleinen XML-Datei in ein Dictionary (und umgekehrt) geschrieben. Leider habe ich ein kleines Problem mit der sauberen Kodierung/Dekodierung von UTF-8 zu darstellbarem String. Die erste XML-Datei ("sample-in.xml") habe ich mir dabei "von Hand" erstellt. Das Ergebnis wird vom Skript erstellt ("sample-out.xml"). Leider schaffe ich es nicht "sample-out.xml" wieder so ein zu lesen, dass alle Sonderzeichen richtig interpretiert werden. Vielleicht kann mir einer von euch ein Tipp geben?
Hier der Code:
"sample-in.xml":
"sample-out.xml":
Vielen Dank im Voraus, Calo
ich habe zwei Funktionen zur Umwandlung einer kleinen XML-Datei in ein Dictionary (und umgekehrt) geschrieben. Leider habe ich ein kleines Problem mit der sauberen Kodierung/Dekodierung von UTF-8 zu darstellbarem String. Die erste XML-Datei ("sample-in.xml") habe ich mir dabei "von Hand" erstellt. Das Ergebnis wird vom Skript erstellt ("sample-out.xml"). Leider schaffe ich es nicht "sample-out.xml" wieder so ein zu lesen, dass alle Sonderzeichen richtig interpretiert werden. Vielleicht kann mir einer von euch ein Tipp geben?
Hier der Code:
Code: Alles auswählen
from xml.dom import minidom
def xml2dict(xml_fn):
"""
Description:
~~~~~~~~~~~~
parses and converts a XML-file (robodoc-format) to an python-dictionary
format.
"""
with open(xml_fn, "rb") as f:
xml_file = minidom.parse(f) #.toxml('utf-8') .decode('utf-8')
rdoc = xml_file.documentElement
xml_robo = {}
node_l = rdoc.firstChild
while node_l:
if node_l.nodeType == node_l.ELEMENT_NODE:
lang = node_l.getAttribute("lang")
xml_robo[lang] = {}
node_k = node_l.firstChild
while node_k:
if node_k.nodeType == node_k.ELEMENT_NODE:
attr = node_k.getAttribute("name")
val = node_k.firstChild.data
xml_robo[lang][attr] = val ; print(val, type(val)) #.decode('utf-8')
node_k = node_k.nextSibling
node_l = node_l.nextSibling
return xml_robo
def dict2xml(dxml, new_fn, base_fn):
"""
Description:
~~~~~~~~~~~~
converts a dictionary (robodoc-format) to a XML-file with robodoc-format.
"""
base_xml_file = minidom.parse(base_fn)
base_doc = base_xml_file.documentElement
node_l = base_doc.firstChild
while node_l:
if node_l.nodeType == node_l.ELEMENT_NODE:
lang = node_l.getAttribute("lang")
node_k = node_l.firstChild
while node_k:
if node_k.nodeType == node_k.ELEMENT_NODE:
attr = node_k.getAttribute("name")
node_k.firstChild.data = dxml[lang][attr].encode()
node_k = node_k.nextSibling
node_l = node_l.nextSibling
# export xml-file to new_fn
with open(new_fn, "w") as f:
f.write(base_xml_file.toxml()) #.toxml('utf-8'))
# ##############################################################################
if __name__ == "__main__":
from pprint import pprint
# dxml = xml2dict(xml_fn="sample-in.xml")
dxml = xml2dict(xml_fn="sample-out.xml")
# pprint(dxml)
#
# dict2xml(dxml=dxml, new_fn="sample-out.xml", base_fn="sample-in.xml")
# print("...done")
#
# print(dxml["en"]["material_description"]) #.decode())
Code: Alles auswählen
<?xml version="1.0" encoding="utf-8"?>
<roboxml>
<language lang="en">
<key name="product">air-intake-manifold</key>
<key name="material_description">Material:
Durethan AKV35 , cond.
Mechanical Properties:
σB = 98 MPa
εB = 1.2 %
Strength Criteria:
σlim = 78 MPa
εlim = 0.9 %</key>
</language>
<language lang="de">
<key name="product">ladeluftsammler</key>
<key name="material_description">Material:
Durethan AKV35 , cond.
Mechanical Properties:
σB = 98 MPa
εB = 1.2 %
Strength Criteria:
σlim = 78 MPa
εlim = 0.9 %</key>
</language>
</roboxml>
Code: Alles auswählen
<?xml version="1.0" ?><roboxml>
<language lang="en">
<key name="product">b'air-intake-manifold'</key>
<key name="material_description">b'Material:\n Durethan AKV35 , cond.\nMechanical Properties:\n \xcf\x83B = 98 MPa\n \xce\xb5B = 1.2 %\nStrength Criteria:\n \xcf\x83lim = 78 MPa\n \xce\xb5lim = 0.9 %'</key>
</language>
<language lang="de">
<key name="product">b'ladeluftsammler'</key>
<key name="material_description">b'Material:\n Durethan AKV35 , cond.\nMechanical Properties:\n \xcf\x83B = 98 MPa\n \xce\xb5B = 1.2 %\nStrength Criteria:\n \xcf\x83lim = 78 MPa\n \xce\xb5lim = 0.9 %'</key>
</language>
</roboxml>
Vielen Dank im Voraus, Calo