UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2: ordinal not in range(128)

Post date: Jul 5, 2016 9:33:35 PM

When I want to display Thai dialogs using command .format() in python, have this error message:

UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2: ordinal not in range(128)

I found a good explanation to this on stackoverflow:

    1. Explicitly encode that unicode in the appropriate character set. For example, if it's UTF-8, do "{}".format(source[1].encode('utf-8')).
    2. Use a unicode format string: u"{}".format(source[1]). You may still need to encode that UserId later; I have no idea how your write_file function works. But it's generally better to keep everything Unicode as long as possible, only encoding and decoding at the very edges, than to try to mix and match the two.