I'm using python code that converts my XML to a CSV file and reads specific fields like "full_name", "item_name", "price", "in_stock". Unfortunately, I have a problem with reading the EAN field. During conversion, he receives the error: "AttributeError: 'NoneType' object has no attribute 'text'". I would like to add that when I remove the EAN code, everything works without any problems. How to modify the code so that it can read the EAN as well? I would be grateful for a specific piece of code that I need to add.
Below is a piece of XML file:
<?xml version="1.0" encoding="UTF-8"?>
<catalogue date="2022-08-23 15:58" GMT= "+1">
<product>
<id>14726</id>
<manufacturer>Kieslect</manufacturer>
<item_name>Kieslect Smart Tag Lite Pack (2 x Black and 1 x White) Black White</item_name>
<sku>157003-126899-18495_HU03</sku>
<warehouse>HU03</warehouse>
<bar_code>157003-126899-18495</bar_code>
<in_stock><![CDATA[&lt;50]]></in_stock>
<exp_delivery><![CDATA[0]]></exp_delivery>
<delivery_date>0000-00-00</delivery_date>
<price>20.00</price>
<image>https://images.bluefinmobileshop.com/1637675528/large-full/kieslect-smart-tag-lite-pack-2-x-black-and-1-x-white-black-white.jpg</image>
<properties> <full_name>Kieslect Smart Tag Lite (6974377570098)</full_name>
<ean>6974377570098</ean>
</properties>
<category>accessory</category>
</product>
</catalogue>
Here is my Python code:
# Importing the required libraries
import xml.etree.ElementTree as Xet
import pandas as pd
cols = ["full_name", "item_name", "price", "in_stock", "ean"]
rows = []
# Parsing the XML file
xmlparse = Xet.parse('in.xml')
root = xmlparse.getroot()
parameters = root.findall('.//product')
for product in parameters:
item_name = product.find("item_name").text
in_stock = product.find("in_stock").text
price = product.find("price").text
sku = product.find("sku").text
for child in product.findall('.//properties'):
full_name = child.find('full_name').text
ean = child.find('ean').text
rows.append({
"full_name": full_name,
"item_name": full_name,
"price": price,
"in_stock": in_stock,
"ean": ean
})
df = pd.DataFrame(rows, columns=cols)
# Writing dataframe to csv
df.to_csv('out.csv', index=False)