Python XML-Dateien lesen?
Hallo,
Wie kann man in Python XML-Dateien lesen. Ich habe mir dafür schon ein paar Tutorials durchgelesen, aber bei dem das ich verwenden möchte sagt mein Python-Interpreter die ganze Zeit:
File "C:\Users\toni-\source\repos\PythonApplication1\PythonApplication1\PythonApplication1.py", line 32, in <module>
value = b_name.get('qty')
AttributeError: 'NoneType' object has no attribute 'get'
Wist ihr woran es liegt?
Hier das Tutorial:
Lesen und Schreiben von XML-Dateien in Python – Acervo Lima
Danke für eine Antwort
TheCPP
Python-Code:
#auslesen Der XML-Datei
with open("conf.xml", 'r') as f:
data = f.read()
bs_data = BeautifulSoup(data, 'xml')
# Finding all instances of tag
b_unique = bs_data.find_all('unique')
print(b_unique)
# Using find() to extract attributes of the first instance of the tag
b_name = bs_data.find('child', {'name':'Acer'})
print(b_name)
# Extracting the data stored in a specific attribute of the `child` tag
value = b_name.get('qty')
print(value)
XML-Code:
<model>
<child name="Acer" qty="12">Acer is a laptop</child>
<unique>Add model number here</unique>
<child name="Onida" qty="10">Onida is an oven</child>
<child name="Acer" qty="7">Exclusive</child>
<unique>Add price here</unique>
<data>
Add content here
<family>Add company name here</family>
<size>Add number of employees here</size>
</data>
</model>
Kannst du vielleicht auch den Code geben, welcher dieser Fehler erzeugt?
Hab den Code ihn die Frage ergänzt
2 Antworten
Dein find() erzeugt ein None-Object, daher die Exception. Laut Doku ist None die Rückgabe für eine erfolglose Suche.
Stellt sich also die Frage, warum die Suche erfolglos ist.
# python soup.py
[<unique>Add model number here</unique>, <unique>Add price here</unique>]
<child name="Acer" qty="12">Acer is a laptop</child>
12
So stellt sich das hier dar, also mußt Du versuchen rauszufinden, was bei Dir schief läuft.
Wist ihr woran es liegt?
Gib doch mal deinen Code ...
Generell liegt es daran, dass deine Variable "b_name" den Wert "None" hat ... da liegt also kein Objekt, keine Daten, dahinter.
Ich habe das Tutorial nicht angeschaut, aber wenn es mehrere Anweisungen sind, wird eben irgendwas bei den vorherigen Anweisungen nicht stimmen - vermutlich bei dir.
Lass dir doch mal zwischen den einzelnen Anweisungen ausgeben, ob der bisherige Code das gewünschte Ergebnis bringt oder schon dort die Fehler kommen. Oder nutze einen Debugger
Python-Code:
#auslesen Der XML-Datei
with open("conf.xml", 'r') as f:
data = f.read()
bs_data = BeautifulSoup(data, 'xml')
# Finding all instances of tag
b_unique = bs_data.find_all('unique')
print(b_unique)
# Using find() to extract attributes of the first instance of the tag
b_name = bs_data.find('child', {'name':'Acer'})
print(b_name)
# Extracting the data stored in a specific attribute of the `child` tag
value = b_name.get('qty')
print(value)
XML-Code:
<model>
<child name="Acer" qty="12">Acer is a laptop</child>
<unique>Add model number here</unique>
<child name="Onida" qty="10">Onida is an oven</child>
<child name="Acer" qty="7">Exclusive</child>
<unique>Add price here</unique>
<data>
Add content here
<family>Add company name here</family>
<size>Add number of employees here</size>
</data>
</model>