Why doesn't installing UnixodeError set a locale?

I have the following Python script:

# -*- coding: utf-8 -*- import sys, locale locale.setlocale(locale.LC_ALL, 'en_US.utf8') print 'θ‚₯ηš‚' # This works print u'θ‚₯ηš‚' 

When running the script, I get:

 θ‚₯ηš‚Traceback (most recent call last): File "../pycli/samples/x.py", line 5, in <module> print u'θ‚₯ηš‚' UnicodeEncodeError: 'latin-1' codec can't encode characters in position 0-1: ordinal not in range(256) 

However, when I explicitly set the LC_ALL environment variable in the shell, it works

 export LC_ALL=en_US.utf8 

So, I wonder why setlocale () doesn't have the same effect?

+6
source share
2 answers

The value is used only to indicate the default encoding for output when the interpreter starts. In other words, you are late after running the script.

+2
source

Unicode is like a conceptual text idea that is only present in your program.

This has the advantage that it can support any character, but a flaw that it cannot output as is, and therefore needs to be encoded into some encoding that can be displayed.

So, you need some input, it will be encoded, and you have to decode it, and if you want to output unicode, you need to code it.

If you do not, python will try to do it for you (using ASCII or something that can be found in your env, as in your case), but you should not rely on it because python may make a mistake (as in your case )

Pretty funny, you may notice that in your case your terminal supports utf8, but this python did not understand that it could use utf8.

This is why you should always encode output and decode input (preferably using utf8 whenever possible!)

You can achieve this by using the unicode encoding method and the string decoding method by providing them with the encoding as an argument.

+1
source

Source: https://habr.com/ru/post/922073/


All Articles