There are 2 different Airflow DAGs that I maintain which generate reports that are delivered via email. And in both reports, the financial information needs to be reported in both USD & EUR currency.
One of the reports involves running a SQL query, which loads the results into a pandas dataframe, then performs a few simple transformations before calling pandas’s to_html()
method for generating the message body. I didn’t realize just how magical that pandas method could be…
…until I realized that the other report was only reporting in USD. And its dataset is a list of tuples. I thought I could just update parts of the code to use the Euro sign (€), but I was very wrong. I kept getting the following error:
UnicodeEncodeError: 'ascii' codec can't encode character u'\u20ac'
That sent me down a Stack Overflow rabbit hole of trying all sorts of things with .encode(‘utf-8’) tacked on to the end of a lot of different strings. And not a single one worked. I then realized that Airflow’s EmailOperator
converts the html_content
to ASCII. The Euro symbol does not exist in ASCII’s 256 characters.
And it was then I stumbled upon a result that showed how to display the Euro sign in various languages. Most of them were some kind of variation of the Unicode value, except for the last one:
HTML: €
Problem solved.