I have a piece of code:
with open('filename.txt','r') as textfile:
kwList = [x.strip('\n') for x in textfile.readlines()]
I get a: UnicodeDecodeError : 'ascii' codec can't decode byte 0xc4 in position 5595: ordinal not in range(128) on line 2
The problem is that according the python docs : https://docs.python.org/3/library/functions.html#open
Python3 uses locale.getpreferredencoding(False) to get the default encoding to use when there is no encoding specified in the open method.
When I run locale.getpreferredencoding(False), I get 'UTF-8'.
Why do I get 'ascii' codec failed in the UnicodeDecodeError when Python should use 'utf-8' to do this?
locale.getpreferredencoding(False)command in the same context.print(locale.getpreferredencoding(False))directly above yourwith open(...) as textfileor via some other means?encodingargument to theopen()call?LC_CTYPEenvironment variable, which if not set explicitly is derived fromLC_ALLorLANG. So if you production code is run withLANG=CorLC_ALL=C, then the default C locale is used which uses ASCII as the encoding.