I get an error in the production system that I cannot reproduce in the development environment:
with io.open(file_name, 'wt') as fd:
fd.write(data)
An exception:
File "/home/.../foo.py", line 18, in foo
fd.write(data)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe4' in position 6400: ordinal not in range(128)
I already tried, but a lot of weird characters in a variable data
.
But so far I have not been able to reproduce UnicodeEncodeError
.
What should be in data
order to receive UnicodeEncodeError
?
Update
python -c 'import locale; print locale.getpreferredencoding()'
UTF-8
Update2
If I call locale.getpreferredencoding()
through the shell and through the web request, the encoding will be "UTF-8".
I updated the exception handling in my code and registered getpreferredencoding()
from some days. Now this happened again (still I can not force or reproduce it), and the encoding is "ANSI_X3.4-1968"!
I have no clue where this encoding is set ...
. . : ? .
, ,