Converting XES file to CSV

In this post we’ll see how to easily convert a XES file to a CSV by using a simple Python script.

Pre-requisites

Before starting

Before running the script you first need to ensure Python is ready to use. For that just type in the command line $ python –version and you should see this result (or similar depending on the version you had installed):

$ python --version
$ Python 3.9.13

Now we need to install the pm4py Python library, for that we’ll use the pip program (still in the command line):

$ pip install pm4py

Now Python is ready and you can now run the script.

Running the Python script

The script below can be copied as is in a text file whereever you want in your machine. Just rename the file as XEStoCSV.py

import pm4py
import pandas as pd
import sys

# First argument is the XES file to convert
if __name__ == "__main__":
	# Chack arguments
	if len( sys.argv ) != 2:
		print( "XES to CSV Python Converter" )
		print( "\tusage: python3 XEStoCSV.py XESFile.xes" )
		exit()
	# Set the output filename
	newfile = sys.argv[1][:-3] + "csv"
	print ( newfile )
	# Convert the file and create a new CSV file
	print ("Converting file:", sys.argv[1], " ..." )
	log = pm4py.read_xes(sys.argv[1])
	log.to_csv(newfile)

This script accepts one argument (mandatory): the XES filename and returns the file converted in a CSV format (with comma separator)

Leave a Comment