Python 将datetime.ctime()值转换为Unicode

Python 将datetime.ctime()值转换为Unicode,python,datetime,unicode,Python,Datetime,Unicode,我想将datetime.ctime()值转换为Unicode 使用在Windows下运行的Python 2.6.4,我可以将语言环境设置为西班牙语,如下所示: >>> import locale >>> locale.setlocale(locale.LC_ALL, 'esp' ) >>>导入区域设置 >>>setlocale(locale.LC_ALL'esp') 然后我可以将%a、%a、%b和%b传递给ctime(),以获取日期和月份名称以及缩写 >>> import datetime >>

我想将datetime.ctime()值转换为Unicode

使用在Windows下运行的Python 2.6.4,我可以将语言环境设置为西班牙语,如下所示:

>>> import locale >>> locale.setlocale(locale.LC_ALL, 'esp' ) >>>导入区域设置 >>>setlocale(locale.LC_ALL'esp') 然后我可以将%a、%a、%b和%b传递给ctime(),以获取日期和月份名称以及缩写

>>> import datetime >>> dateValue = datetime.date( 2010, 5, 15 ) >>> dayName = dateValue.strftime( '%A' ) >>> dayName 's\xe1bado' >>>导入日期时间 >>>dateValue=datetime.date(2010,5,15) >>>dayName=dateValue.strftime(“%A”) >>>名字 's\xe1bado' 如何将's\xe1bado'值转换为Unicode?具体来说,我使用什么编码

我想我可能会做如下的事情,但我不确定这是正确的方法

>>> codePage = locale.getdefaultlocale()[ 1 ] >>> dayNameUnicode = unicode( dayName, codePage ) >>> dayNameUnicode u's\xe1bado' >>>codePage=locale.getdefaultlocale()[1] >>>dayNameUnicode=unicode(dayName,代码页) >>>日名Unicode u's\xe1bado'
马尔科姆

这可能取决于您的操作系统。但数据看起来像拉丁语1

>>> s.decode('latin1')
u's\xe1bado'

使用示例中的
unicode()
string.decode()
进行转换应该可以。唯一的问题应该是,在您的示例中,您使用默认语言环境的编码,即使您之前将语言环境设置为不同的内容。如果您使用
locale.getlocale()[1]
而不是
locale.getdefaultlocale()[1]
,您应该会得到正确的结果。

它是Unicode-当您对其调用
Unicode()
时,它变成了Unicode。您可以分辨出来,因为当使用
repr()
显示字符串时,字符串前面有一个
u
。请尝试打印

>>> d = u's\xe1bado'
>>> d
u's\xe1bado'
>>> print d
sábado
>>>

这就是unicode,您只需打印字节而不是字符。感谢所有回复者。Daniel你是对的-我已经正确地转换为Unicode,但是在校对代码时,我正在查看repr与输出版本的值。