如何在Python中获取unicode月份名称?

如何在Python中获取unicode月份名称?,第1张

概述我想获得一个unicode版本的calendar.month_abbr [6].如果我没有为语言环境指定编码,我不知道如何将字符串转换为unicode.下面的示例代码显示了我的问题: >>> import locale>>> import calendar>>> locale.setlocale(locale.LC_ALL, ("ru_RU"))'ru_RU'>>> print repr( 我想获得一个unicode版本的calendar.month_abbr [6].如果我没有为语言环境指定编码,我不知道如何将字符串转换为unicode.下面的示例代码显示了我的问题:

>>> import locale>>> import calendar>>> locale.setlocale(locale.LC_ALL,("ru_RU"))'ru_RU'>>> print repr(calendar.month_abbr[6])'\xb8\xee\xdd'>>> print repr(calendar.month_abbr[6].decode("utf8"))Traceback (most recent call last):  file "<stdin>",line 1,in <module>  file "/usr/lib/python2.5/enCodings/utf_8.py",line 16,in decode    return codecs.utf_8_decode(input,errors,True)UnicodeDecodeError: 'utf8' codec can't decode byte 0xb8 in position 0: unexpected code byte>>> locale.setlocale(locale.LC_ALL,("ru_RU","utf8"))'ru_RU.UTF8'>>> print repr(calendar.month_abbr[6])'\xd0\x98\xd1\x8e\xd0\xbd'>>> print repr(calendar.month_abbr[6].decode("utf8"))u'\u0418\u044e\u043d'

任何想法如何解决这个问题?解决方案不必看起来像这样.任何给我在unicode中缩写月份名称的解决方案都很好.

解决方法 更改代码中的最后一行:

>>> print calendar.month_abbr[6].decode("utf8")Июн

使用不当的repr()隐藏你已经得到你需要的东西.

getlocale()也可用于获取当前语言环境的编码:

>>> locale.setlocale(locale.LC_ALL,'en_US')'en_US'>>> locale.getlocale()('en_US','ISO8859-1')

另一个可能对您有用的模块:

> PyICU – 更好的国际化方式.虽然locale根据 *** 作系统中的语言环境数据库生成初始或变形的月份名称(因此您不能依赖俄语这样的语言!)并使用一些编码,PyICU具有不同的初始和变形格式说明符(所以你可以在你的情况下选择合适的)并使用unicode.
> pytils – 一套使用俄语的工具,包括日期.它具有硬编码的月份名称作为区域设置限制的解决方法.

总结

以上是内存溢出为你收集整理的如何在Python中获取unicode月份名称?全部内容,希望文章能够帮你解决如何在Python中获取unicode月份名称?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1194442.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-03
下一篇2022-06-03

发表评论

登录后才能评论

评论列表(0条)

    保存