gpt4 book ai didi

python - 为什么 locale.getpreferredencoding() 返回 'ANSI_X3.4-1968' 而不是 'UTF-8' ?

转载 作者:太空狗 更新时间:2023-10-30 01:26:34 26 4
gpt4 key购买 nike

每当我尝试使用 open(file_name, encoding='utf-8') 读取 UTF-8 编码的文本文件时,我总是会收到一条错误消息,提示 ASCII 编解码器无法解码某些字符(例如,当使用 for line in f: print(line) 时)

Python 3.5.3 (default, Jan 19 2017, 14:11:04)
[GCC 6.3.0 20170118] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import locale
>>> locale.getpreferredencoding()
'ANSI_X3.4-1968'
>>> import sys
>>> sys.getfilesystemencoding()
'ascii'
>>>

locale命令输出:

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
LANG=en_US.UTF-8
LANGUAGE=
LC_CTYPE=en_HK.UTF-8
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=

最佳答案

我遇到了类似的问题。对我来说,最初没有设置环境变量LANG(你可以通过运行env来检查)

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

我可用的语言环境是(在新的 Ubuntu 18.04 Docker 镜像上):

$ locale -a
C
C.UTF-8
POSIX

所以我选择了 utf-8:

$ export LANG="C.UTF-8"

然后一切正常

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

如果您选择了一个不可用的语言环境,例如

export LANG="en_US.UTF-8"

它将工作:

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

这就是 locale 给出错误信息的原因:

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

关于python - 为什么 locale.getpreferredencoding() 返回 'ANSI_X3.4-1968' 而不是 'UTF-8' ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44344458/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com