在ubuntu:
> s = 'kasım' # ı -> 'i' without dot, lowercase letter, turkish.
> print s
> 'kas\xc4\xb1m'
> unicode(s, 'utf-8') 效果很好。
在窗户上:
> s = 'kasım' # ı -> 'i' without dot, lowercase letter, turkish.
> print s
> 'kas\x8dm'
> unicode(s, 'utf-8') 抛出一个单解码错误;
在此之前,区域设置的代码如下所示:
if platform is windows:
locale_to_set = 'turkish'
elif platform is linux:
locale_to_set = 'tr_TR.utf-8'
locale.setlocale(locale.LC_ALL, locale_to_set)我做错了什么或者错过了什么?任何想法都会感激的。
注意:
我从datetime.datetime.utcnow().strftime(.)处得到'Kasım‘这个词(意思是11月)。用户可以根据喜好改变语言。
发布于 2013-11-18 12:52:22
依赖于系统的输入编码是个坏主意,因为正如您发现的那样,这些编码可能因系统而异。因此,最好避免源代码中的非ASCII字符,并使用符号名称。例如:
name = u'kas\u0131m'如果您的字符串来自系统的其他地方,例如来自本地化的strftime函数,则在将其解码为Unicode时,您需要使用适当的区域设置:
ignore, encoding = locale.getlocale()
name = unicode(s, encoding)https://stackoverflow.com/questions/20048218
复制相似问题