Wednesday, March 03, 2021

My Code Needed a Comment to Compile

 When I was learning python, I added a utf-8 string literal to the source code as follows:

# replace invalid ’ character with '
inputFileContents = inputFileContents.replace('’', "'")

Which caused this error:

SyntaxError: Non-ASCII character '\xe2' in file gcnlsync.py on line 85, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details

My python code would not run until I added a comment to the file that declared utf-8 encoding. This is literally the top 2 lines of my file:

#!/usr/bin/python
# This Python file uses the following encoding: utf-8

After adding the comment, it ran perfectly! 😳