When integrating with Zalando channel and using Tradebyte services to connect your catalog, you must adhere to a brand readiness checklist provided by Tradebyte.

This Python script is designed to transform CSV data into an XML format based on a specific XML schema. It reads data from a source CSV file and generates an XML file structured according to the format of the destination XML object.

Github repo: Tradebyte-CSV-To-XML-with-Python

Prerequisites

Before using this script, ensure you have the following:

  • Python installed on your system (Python 3.x recommended). You can download Python from the official website: Python Downloads.
  • Necessary Python modules, including csvtime and xml.etree.ElementTree, which are typically included with Python.
  • A source CSV file containing the data to be transformed Attached is one, but you can request a sample from your Tradebyte Solution Delivery Manager during the integration.
  • Zalando.de sign is zade The Tradebyte info center contains the complete channels information for Zalando(de, at, it, fr….), Log in with your account and navigate to Manuals > Channel-specific Manuals > Data maintenance for Zalando > Quick Start Guide.

Import Necessary Libraries

We begin by importing two key libraries: csv , timeand xml.etree.ElementTree. The csv library helps us read the source CSV file, time & datetime used to provides functions for working with time-related operations while xml.etree.ElementTree allows us to create and manipulate XML data structures.

import csv
import xml.etree.ElementTree as ET
import time
from datetime import datetime

Reading the Source CSV Data

Next, we open the source CSV file for reading and use csv.DictReader to read it as a dictionary. This allows us to access CSV columns by their headers.

with open('source1.csv', mode='r', newline='', encoding='utf-8-sig') as csv_file:
    csv_reader = csv.DictReader(csv_file)

Why i have used utf-8-sig instead of utf-8? because i was getting a key error for row[“p_brand”] which it would be used later on.

row[“p_brand”] contains your Brand name which is located as the first row in your csv file. Every CSV file saved with UTF-8 encoding and had a silent ‘\ufeff’ prefix.

The ‘\ufeff’ prefix indicates a Byte Order Mark (BOM) character that is sometimes added when saving a CSV file with UTF-8 encoding in some software. To handle this issue, you can specify the encoding when reading the CSV file using the utf-8-sig encoding, which will automatically handle the BOM character.

Create the XML Structure

We start building the XML structure by creating the root element and adding attributes as specified in the destination XML format.

current_timestamp = str(int(time.time()))
root = ET.Element("TBCATALOG", version="1.3", creation=current_timestamp)

In Python, the functions is `time.time()`, which returns the current time in seconds.

The `int()` function is used to convert the floating-point result returned by `time.time()` into an integer.

The `str()` function is then used to convert the integer value into a string.

Finally, the result of `str(int(time.time()))` is assigned to the variable `current_timestamp`, which holds the current time as a string, represented as the number of seconds.

you can convert the creation time online and find out when i generated the XML Panda catalogue here.

Create PRODUCTDATA and PRODUCT elements

product_data = ET.SubElement(root, "PRODUCTDATA", type="full")
product = ET.SubElement(product_data, "PRODUCT")

Iterate Through CSV Rows

Now, we iterate through each row of the source CSV data using a for loop. Inside the loop, we create XML elements and add data to them based on the desired structure.

for row in csv_reader:
    # Create XML elements and add data
    p_nr = ET.SubElement(product, "P_NR")
    p_nr.text = row["p_nr"]
    # ... (repeat this for other elements)