How to print special characters like Beta, alpha, Phi while using CSV writer in Python

This is my second blog post and gosh, I am loving it here.

So here it goes. I had a requirement where I had to print the special greek characters like Alpha, beta, omega, Phi etc in excel using CSV module.

Since I am pretty new to the platform, I searched around in the wonderful community of python and realized that I need to open the file with utf-8 encoding for writing the .csv file.

Sample code below where I am printing the beta and phi characters using the unicode values of those characters.

import csv

with open(’emissions.csv’, ‘w’, encoding=”utf-8″, newline=”) as csvfile:
     filewriter = csv.writer(csvfile, delimiter=’\t’,
                             quotechar=’\u01C0′, quoting=csv.QUOTE_MINIMAL)
     filewriter.writerow([”, ‘Natural Gas’, ‘Diesel’, ‘Fuel oil’, ‘Coke’])
     filewriter.writerow([‘NHV (MJ/Kg)’, ‘51.2’, ’42’, ’40’, ‘30.0’])
     filewriter.writerow([‘\u03B2’, ‘0.76’, ‘0.87’, ‘0.85’, ‘0.74’]) – unicode for beta
     filewriter.writerow([‘\u03D5’, ‘3.5’, ‘3.7’, ‘3.7’, ‘5.3’]) – unicode for Phi
     filewriter.writerow([‘CER’, ‘0.052’, ‘0.077’, ‘0.079’, ‘0.131’])
     filewriter.writerow([‘COST($/KG)’, ‘0.34’, ‘0.33’, ‘0.28’, ‘0.04’])

Unfortunately when I ran the code, the excel file didn’t have the correct characters printed. As you can see from the below screenshot, the characters didn’t get printed at all.

image

I am quite proficient in c# having worked on the platform for the last 12 years. Using the same logic with which I would have done there, instead of using the utf-8 encoding, I used the utf-32 encoding this time. The characters printed fine. However the issue now happened is all the column values were showing up in a single excel column instead of separate columns in excel.

image

I was close and on seeing this I realized, this may be due to delimiter I am using for my .csv file. So instead of using comma as the delimiter, I started using tab (\t) as the delimiter.

image

And now it worked just fine.

Hope this helps!

Debajit Dutta

(Microsoft MVP & MCT)


Discover more from Debajit's Power Apps & Dynamics 365 Blog

Subscribe to get the latest posts sent to your email.

Discover more from Debajit's Power Apps & Dynamics 365 Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading