Windows Python:使用语言环境模块更改编码 [英] Windows Python: Changing encoding using the locale module
问题描述
使用Python 2.7
Using Python 2.7
我正在写一个抽象的Web刮板,在显示(打印)某些字符时遇到问题.
I am writing an abstract web scraper and am having problems when displaying (printing) certain characters.
我得到追溯错误: UnicodeEncodeError:'ascii'编解码器无法在位置5编码字符u'\ u2606':序号不在范围(128)内
角色.
I get the trace-back error: UnicodeEncodeError: 'ascii' codec can't encode character u'\u2606' in position 5: ordinal not in range(128)
from printing the string containing the character.
我使用语言环境模块查找操作系统支持的设置,尽管我不确定我是否应该使用语言环境解决问题,并且注意到默认设置为(en_US','cp1252')
.我正在尝试将其更改为('en_US','utf-8')
,但遗憾的是无济于事.
I used the locale module to find out my OS supported settings, although I'm not certain I should use locale for my problem, and noticed the default settings where (en_US', 'cp1252')
. I am trying to change it to ('en_US', 'utf-8')
but sadly to no avail.
#code for default settings
print locale.getdefaultlocale()
这是我用来缩小语言环境设置选项的代码.(这里没有问题,代码只是任何人想要的,都可以遵循)
This is the code I used to narrow down my locale setting options. ( No problems here, the code is just so anyone that wants to, can follow along )
import locale
all = locale.locale_alias().items()
utfs = [(k,v) for k,v in all if 'utf' in k.lower() or 'utf' in v.lower()]
# utf settings starting with en
en_utfs = [(k,v) for k,v in utfs if k.lower()[:2].lower() == 'en' or
v.lower()[:2] == 'en'
print en_utfs
这给出了输出:
[('en_ie.utf8@euro', 'en_IE.UTF-8'), ('universal.utf8@ucs4', 'en_US.UTF-8')]
这是我的问题所在;尝试将设置更改为 en_US.UTF-8
.
Here is where my problem lies; with trying to change the setting to en_US.UTF-8
.
[IN]: locale.setlocale( locale.LC_ALL, 'en_US.UTF-8' )
[OUT]: Traceback code ...
[OUT]: locale.Error: unsupported locale setting
对不起所有代码,出于某种原因,我感到这样做的必要性过多.
Sorry for all the code, for some reason I felt the excessive need to do so.
推荐答案
选中此 https://docs.moodle.org/dev/Table_of_locales
我认为在Windows中,您需要设置"localewin"值而不是语言环境名称.在Windows中设置 locale.setlocale(locale.LC_ALL,'English_United States.1252')
对我来说很有效.我还尝试设置了不同的语言环境 Dutch_Netherlands.1252
并且它们起作用了.尽管这可能无法解决您的UnicodeEncodeError问题,但我认为此问题至少说明了您无法设置语言环境的原因.
I think in windows you need to set 'localewin' value instead of the locale name. Setting locale.setlocale( locale.LC_ALL, 'English_United States.1252' )
worked for me in windows. I also tried setting different locales Dutch_Netherlands.1252
and they worked. Though this might not solve your problem of UnicodeEncodeError, but I think this atleast explains why you are unable to set the locale.
这篇关于Windows Python:使用语言环境模块更改编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!