Свести XML в формат CSV String Python

Привет, поэтому у меня есть следующий код XML

<?xml version="1.0"?>
<?xml-stylesheet href="catalog.xsl" type="text/xsl"?>
<!DOCTYPE catalog SYSTEM "catalog.dtd">
<catalog>
    <product description="Cardigan Sweater" product_image="cardigan.jpg">
        <catalog_item gender="Men's">
            <item_number>QWZ5671</item_number>
            <price>39.95</price>
            <size description="Medium">
                <color_swatch image="red_cardigan.jpg">Red</color_swatch>
                <color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
            </size>
            <size description="Large">
                <color_swatch image="red_cardigan.jpg">Pink</color_swatch>
                <color_swatch image="burgundy_cardigan.jpg">Grey</color_swatch>
            </size>
        </catalog_item>
        <catalog_item gender="Women's">
            <item_number>RRX9856</item_number>
            <price>42.50</price>
            <size description="Small">
                <color_swatch image="red_cardigan.jpg">Purple</color_swatch>
                <color_swatch image="navy_cardigan.jpg">Navy</color_swatch>
                <color_swatch image="burgundy_cardigan.jpg">Green</color_swatch>
            </size>
            <size description="Medium">
                <color_swatch image="red_cardigan.jpg">orange</color_swatch>
                <color_swatch image="navy_cardigan.jpg">yellow</color_swatch>
                <color_swatch image="burgundy_cardigan.jpg">black</color_swatch>
                <color_swatch image="black_cardigan.jpg">white</color_swatch>
            </size>
            <size description="Large">
                <color_swatch image="navy_cardigan.jpg">parrot green</color_swatch>
                <color_swatch image="black_cardigan.jpg">Blue</color_swatch>
            </size>
            <size description="Extra Large">
                <color_swatch image="burgundy_cardigan.jpg">Aqua</color_swatch>
                <color_swatch image="black_cardigan.jpg">violet</color_swatch>
            </size>
        </catalog_item>
    </product>
</catalog>

Мой ожидаемый результат примерно такой

,,,QWZ5671,39.95,,red
,,,QWZ5671,39.95,,pink
.
.
.
,,,RRX9856,42.50,,violet  \\out put returns me only this value

Но я могу получить только последний ряд (см. Пример выше). Это то, что я сделал.

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

tree = ET.parse('sample.xml')
root = tree.getroot()


def f(elem, result):
    result[elem.tag] = elem.text
    cs = elem.getchildren()
    for c in cs:
        result = f(c, result)
    return result


d = f(root, {})
print(d)

0 ответов

Другие вопросы по тегам