Why does locale.getpreferredencoding() return 'ANSI_X3.4-1968' instead of 'UTF-8'?

前端 未结 3 774
旧巷少年郎
旧巷少年郎 2020-12-16 23:27

Whenever I try to read UTF-8 encoded text files, using open(file_name, encoding=\'utf-8\'), I always get an error saying ASCII codec can\'t decode some characte

3条回答
  •  一整个雨季
    2020-12-17 00:00

    I had a similar problem. For me, initially the environtment variable LANG was not set (you can check this by running env)

    $ python3 -c 'import locale; print(locale.getdefaultlocale())'
    (None, None)
    $ python3 -c 'import locale; print(locale.getpreferredencoding())'
    ANSI_X3.4-1968
    

    The available locales for me was (on a fresh Ubuntu 18.04 Docker image):

    $ locale -a
    C
    C.UTF-8
    POSIX
    

    So i picked the utf-8 one:

    $ export LANG="C.UTF-8"
    

    And then things work

    $ python3 -c 'import locale; print(locale.getdefaultlocale())'
    ('en_US', 'UTF-8')
    $ python3 -c 'import locale; print(locale.getpreferredencoding())'
    UTF-8
    

    If you pick a locale that is not avaiable, such as

    export LANG="en_US.UTF-8"
    

    it will not work:

    $ python3 -c 'import locale; print(locale.getdefaultlocale())'
    ('en_US', 'UTF-8')
    $ python3 -c 'import locale; print(locale.getpreferredencoding())'
    ANSI_X3.4-1968
    

    and this is why locale is giving the error messages:

    locale: Cannot set LC_CTYPE to default locale: No such file or directory
    locale: Cannot set LC_ALL to default locale: No such file or directory
    

提交回复
热议问题