我需要改变主意
14 Şubat 2015 Cumartesi, 09:47:49
迄今为止。当我打印这个日期时,它工作得很好,但我无法更改time.strptime
内部的编码,无论我尝试编码或解码为不同的类型。这是我的密码
# -*- coding: cp1254 -*-
import chardet
import time
from time import mktime
import datetime
h="14 Şubat 2015 Cumartesi, 09:47:49"
kc= datetime.datetime.fromtimestamp(mktime(time.strptime(h.decode('utf-8), "%d %B %Y %A,%H:%M:%S") ))
print kc
打印chardet.检测(h) 结果是
^{pr2}$
假设您正试图用土耳其日期的字符串表示创建一个
datetime
对象。在您需要做的第一件事是将文件的源代码编码从
cp1254
更改为utf-8
,这基本上涵盖了更广泛的字符集。在其次,您应该将语言环境设置为
^{pr2}$tr_TR
,以便Python在创建日期对象时理解Şubat
的含义。在然后,可以执行以下操作将日期字符串转换为实际的
datetime.datetime
对象。在希望这有帮助。在
h应为unicode字符串:
我认为您需要将行改为:
^{pr2}$(无法验证,因为我不知道区域设置,而且由于它我得到了错误)
确保您的区域设置为标识}
Şubat
和{有:
找到这个解决方案是件痛苦的事。这是一个Windows解决方案。从你的帖子里不清楚你在用什么操作系统。经验教训:
trk
或{cp1254
。getlocale()
表示正确的编码。https://docs.python.org/2/library/datetime.html(底部注1)。在strptime
中有一个错误,它不接受ubat,而是接受ubat。解决方案是在处理之前.lower()
字符串。Python2和3的months大写版本也有问题。在Python解决方案
下面是
.lower()
解决方案。我特别使用了utf-8
的源代码来明确strptime
使用的字符串必须是正确的cp1254
编码。在输出:
^{pr2}$Python 3.4.2解决方案
python3默认使用Unicode来处理所有事情,这使得事情变得更简单,Şubat的情况问题也得到了修复。在
输出:
^{pr2}$相关问题 更多 >
编程相关推荐