Python UnicodeDecodeError: 'ascii' codec can't decode byte

...
outfile.write(line.encode('utf-8'))
UnicodeDecodeError: 'ascii' codec can't decode byte 0x94 in position 32: ordinal not in range(128)

I found this a good explanation regarding this.

read more here:

https://stackoverflow.com/questions/21129020/how-to-fix-unicodedecodeerror-ascii-codec-cant-decode-byte

There are several solutions:

For Python 2

u'Zurich'
# encoding: utf-8
import io
with io.open("my_utf8_file.txt", "r", encoding="utf-8") as my_file:
    my_unicode_string = my_file.read()

    charset='utf8',
    use_unicode=True
db = MySQLdb.connect(host="localhost", user='root', passwd='passwd', db='sandbox', use_unicode=True, charset="utf8")
psycopg2.extensions.register_type(psycopg2.extensions.UNICODE)
psycopg2.extensions.register_type(psycopg2.extensions.UNICODEARRAY)

For Python 3

In Python 3 the regular str is a Unicode string.

With .decode() you can decode a byte string to UTF-8.

open() operates by default in text mode, so returns decoded unicode str.