I am new to Python and have a file.xml with the following structure:
<?xml version="1.0" encoding="UTF-8"?>
<HEADER>
<PRODUCT_DETAILS>
<DESCRIPTION_SHORT>blue dog w short hair</DESCRIPTION_SHORT>
<DESCRIPTION_LONG>blue dog w short hair and unlimitied zoomies</DESCRIPTION_LONG>
</PRODUCT_DETAILS>
<PRODUCT_FEATURES>
<FEATURE>
<FNAME>Hair</FNAME>
<FVALUE>short</FVALUE>
</FEATURE>
<FEATURE>
<FNAME>Colour</FNAME>
<FVALUE>blue</FVALUE>
</FEATURE>
<FEATURE>
<FNAME>Legs</FNAME>
<FVALUE>4</FVALUE>
</FEATURE>
</PRODUCT_FEATURES>
</HEADER>
I am using a very simple snippet (below) to turn it into file_export.csv:
import pandas as pd
df = pd.read_xml("file.xml")
# df
df.to_csv("file_export.csv", index=False)
The problem is that I end up with a table like this:
DESCRIPTION_SHORT DESCRIPTION_LONG FEATURE
blue dog w short hair blue dog w short hair and unlimitied zoomies NaN
I tried removing the FEATURE attribute but ended up overwriting(?) previous FNAME and FVALUE with the last one, assuming because they are called the same:
DESCRIPTION_SHORT DESCRIPTION_LONG FNAME FVALUE
blue dog w short hair blue dog w short hair and unlimitied zoomies None NaN
None None Legs 4.0
What do I need to add to my code to show the nested attributes including their text? Like this:
DESCRIPTION_SHORT DESCRIPTION_LONG FEATURE FNAME FVALUE
blue dog w short hair blue dog w short hair and unlimitied zoomies NaN Hair short
blue dog w short hair blue dog w short hair and unlimitied zoomies NaN Colour blue
blue dog w short hair blue dog w short hair and unlimitied zoomies NaN Legs 4
Thank you in advance!!
~ C