在Ubuntu 12.04中为python设置UTF-8语言环境

时间:2014-05-29 19:57:24

标签: python ubuntu utf-8 locale

在Ubuntu 12.04 VM上(使用vagrant和hashicorp/precise64框设置),我的语言环境说我有UTF-8语言,但是python正在获得latin-1环境。 / p>

这是我所看到的:

vagrant@vagrant:~$ locale                                                                                                                                 
LANG=en_US.UTF-8                                                                                                                                          
LANGUAGE=                                                                                                                                                 
LC_CTYPE="en_US"                                                                                                                                          
LC_NUMERIC="en_US"                                                                                                                                        
LC_TIME="en_US"                                                                                                                                           
LC_COLLATE="en_US"                                                                                                                                        
LC_MONETARY="en_US"                                                                                                                                       
LC_MESSAGES="en_US"                                                                                                                                       
LC_PAPER="en_US"                                                                                                                                          
LC_NAME="en_US"                                                                                                                                           
LC_ADDRESS="en_US"                                                                                                                                        
LC_TELEPHONE="en_US"                                                                                                                                      
LC_MEASUREMENT="en_US"                                                                                                                                    
LC_IDENTIFICATION="en_US"                                                                                                                                 
LC_ALL=en_US                                                                                                                                              
vagrant@vagrant:~$ python                                                                                                                                 
Python 2.7.3 (default, Feb 27 2014, 19:58:35)                                                                                                             
[GCC 4.6.3] on linux2                                                                                                                                     
Type "help", "copyright", "credits" or "license" for more information.                                                                                    
>>> print u'\u1f41'                                                                                                                                       
Traceback (most recent call last):                                                                                                                        
  File "<stdin>", line 1, in <module>                                                                                                                     
UnicodeEncodeError: 'latin-1' codec can't encode character u'\u1f41' in position 0: ordinal not in range(256)

如何为python获得真正的utf-8系统环境?

1 个答案:

答案 0 :(得分:3)

LC_CTYPE的区域设置应该在en_US.UTF-8输出中locale。尝试

export LC_ALL="en_US.UTF-8"

如果它不起作用(如LC_CTYPE明确设置),也是:

export LC_CTYPE="en_US.UTF-8"