Python: ‘NoneType’ object has no attribute ‘text’, XML Parsing

I am trying to take an XML file with Spirit, parse data, and output as a csv file. I feel like I am overlooking something easy. I am having an error:

Traceback (most recent call last):
  File "xml2csv.py", line 12, in <module>
    name = i.find("spirit:name").text
AttributeError: 'NoneType' object has no attribute 'text'

Sample XML File:

<?xml version="1.0" encoding="utf-8"?>
<spirit:component xmlns="http://www.spiritconsortium.org/XMLSchema/SPIRIT/1.5"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:spirit="http://www.spiritconsortium.org/XMLSchema/SPIRIT/1.5"
  xsi:schemaLocation="http://www.spiritconsortium.org/XMLSchema/SPIRIT/1.5 http://www.spiritconsortium.org/XMLSchema/SPIRIT/1.5/memoryMap.xsd">
>
<spirit:generic>
<spirit:name>GENERIC_NAME</spirit:name>
<spirit:description>GENERIC_DESCRIPTION</spirit:description>
</spirit:generic>
</spirit:component>

My Python Code:

# Importing the required libraries
import xml.etree.ElementTree as Xet
import pandas as pd
  
cols = ["name", "description"]
rows = []
  
# Parsing the XML file
xmlparse = Xet.parse('xml_sample.xml')
root = xmlparse.getroot()
for i in root:
    name = i.find("spirit:name").text
    description = i.find("spirit:description").text
  
    rows.append({"spirit:name": name,
                 "spirit:description": description})
  
df = pd.DataFrame(rows, columns=cols)
  
# Writing dataframe to csv
df.to_csv('output.csv')

I suspected my error was in my “.text” based on some other threads I was reading. However, removal of this leads to my .csv file not showing any data.

CSV:

,name,description
0,,

Any suggestions would be greatly appreciated. Kinda stuck on this.

Answer

The below seems to work. Pay attention to the namespace that the code uses: {http://www.spiritconsortium.org/XMLSchema/SPIRIT/1.5}

import xml.etree.ElementTree as ET
import pandas as pd

xml = '''<?xml version="1.0" encoding="utf-8"?>
<spirit:component xmlns="http://www.spiritconsortium.org/XMLSchema/SPIRIT/1.5"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:spirit="http://www.spiritconsortium.org/XMLSchema/SPIRIT/1.5"
  xsi:schemaLocation="http://www.spiritconsortium.org/XMLSchema/SPIRIT/1.5 http://www.spiritconsortium.org/XMLSchema/SPIRIT/1.5/memoryMap.xsd">
>
<spirit:generic>
<spirit:name>GENERIC_NAME</spirit:name>
<spirit:description>GENERIC_DESCRIPTION</spirit:description>
</spirit:generic>
</spirit:component>'''

cols = ["name", "description"]
rows = []
root = ET.fromstring(xml)
names = [x.text for x in root.findall('.//{http://www.spiritconsortium.org/XMLSchema/SPIRIT/1.5}name')]
descriptions = [x.text for x in root.findall('.//{http://www.spiritconsortium.org/XMLSchema/SPIRIT/1.5}description')]
for entry in zip(names, descriptions):
    rows.append({'name': entry[0], 'description': entry[1]})

df = pd.DataFrame(rows, columns=cols)
print(df)

output

           name          description
0  GENERIC_NAME  GENERIC_DESCRIPTION