import csv import xml.etree.ElementTree as ET w = csv.writer(open("catalog.csv", "w", newline="", encoding="utf-8")); w.writerow(["sku", "title", "price", "stock", "tags"]) for p in ET.parse("catalog.xml").getroot().findall("product"): st = p.find("stock") w.writerow([p.get("sku"), p.findtext("title"), p.findtext("price"), "" if st is None else st.text, ";".join(t.text for t in p.findall("tag"))])