在Python中设置语言环境和字符串模块 [英] Setting locale, and string module in Python

查看:107
本文介绍了在Python中设置语言环境和字符串模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个简单的评论:

from locale import LC_ALL, setlocale
print setlocale(LC_ALL,"")
from string import letters
print letters

将以下输出提供给我:

tr_TR.utf8
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ

而且,在这里: http://docs.python .org/library/string.html?highlight = string#string.letters 它表示其值取决于语言环境,并在调用setlocale时更新.但是,我没有看到任何来自我的语言环境的信.有什么方法可以获取当前语言环境的字母列表?

And, in here: http://docs.python.org/library/string.html?highlight=string#string.letters it says that it's value is locale dependent, and updated when setlocale is called. However, I am not seeing any letter from my locale. Is there any way that I could get list of letter for current locale?

推荐答案

我必须将语言环境显式设置为土耳其语,因为这不是计算机上的默认语言,但这似乎或多或少地起作用:

I had to explicitly set the locale to Turkish since that isn't the default on my computer, but it seems to more-or-less work:

> python
Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.

>>> from locale import LC_ALL, setlocale
>>> print setlocale(LC_ALL,"Turkish")
Turkish_Turkey.1254
>>> from string import letters
>>> print letters
abcdefghijklmnopqrstuvwxyzƒsoªµºßàáâaäåæçèéêëìíîïgñòóôoöoùúûüisÿ...
  ABCDEFGHIJKLMNOPQRSTUVWXYZSOYAAAAÄÅÆÇEÉEEIIIIGÑOOOOÖOUUUÜIS
>>>

除了包含Q,W和X之外,输出基本上看起来是正确的(AFAIK),这从维基百科文章不是土耳其字母的一部分.

The output basically looks correct (AFAIK) except for the inclusion of Q, W, and X, which from what's in this Wikipedia article aren't part of the Turkish alphabet.

更新:

为了更好地复制您的环境,我首先使用区域和语言选项"控制面板,并将区域更改为土耳其语",这应该将其设置为setlocale的默认设置.确实确实如此,但是字母列表看起来仍然可以—因此我无法重现您的问题.

To better replicate your environment, I first used the "Regional and Language Options" control panel and changed my region to "Turkish", which should make it the default for setlocale. Indeed it did however the list of letters still looks OK — so I can't reproduce your problem.

这一次的区别是,在运行python之前,我首先将控制台的代码页更改为Windows ANSI Turkish 1254,以启用正确显示字母字符的功能.这样可以使输出的最后两个字母正确显示,但是它仍然包含Q,W和X字母,这些字母不是字母的一部分(在我的选择中有误).

One difference this time is that before running python I first changed to console's code page to Windows ANSI Turkish 1254 to enable the correct display of character from the alphabet. This made the last two letters of the output display correctly, however it also still includes the Q, W, and X letters which aren't part of the alphabet (and wrong to be there, in my option).

C:\>chcp 1254
Active code page: 1254

C:\>python
Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from locale import LC_ALL, setlocale
>>> print setlocale(LC_ALL,"")
Turkish_Turkey.1254
>>> from string import letters
>>> print letters
abcdefghijklmnopqrstuvwxyzƒšœªµºßàáâãäåæçèéêëìíîïğñòóôõöøùúûüışÿ...
  ABCDEFGHIJKLMNOPQRSTUVWXYZŠŒŸÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏĞÑÒÓÔÕÖØÙÚÛÜİŞ
>>>

这篇关于在Python中设置语言环境和字符串模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆