【问题标题】:Calendar: day/month names in specific locale日历:特定语言环境中的日/月名称
【发布时间】:2012-10-13 19:03:15
【问题描述】:

我正在使用标准库中的 Python calendar 模块。基本上我需要一个月中所有日子的列表,如下所示:

>>> import calendar
>>> calobject = calendar.monthcalendar(2012, 10)
>>> print calobject
[[1, 2, 3, 4, 5, 6, 7], [8, 9, 10, 11, 12, 13, 14], [15, 16, 17, 18, 19, 20, 21], [22, 23, 24, 25, 26, 27, 28], [29, 30, 31, 0, 0, 0, 0]]

现在我还需要特定语言环境中月份和日期的名称。我没有找到从calobject 本身获取这些的方法 - 但我能够像这样获取它们:

>>> import calendar
>>> calobject = calendar.LocaleTextCalendar(calendar.MONDAY, 'de_DE')
>>> calobject.formatmonth(2012, 10)
'    Oktober 2012\nMo Di Mi Do Fr Sa So\n 1  2  3  4  5  6  7\n 8  9 10 11 12 13 14\n15 16 17 18 19 20 21\n22 23 24 25 26 27 28\n29 30 31\n'

所以Oktober 是十月的de_DE 名称。美好的。信息必须在那里。我想知道我是否可以在普通的calendar 对象而不是calendar.LocaleTextCalendar 对象上以某种方式访问​​该月份名称。第一个示例(带有列表)确实是我需要的,我不喜欢创建两个日历对象来获取本地化名称的想法。

有人有聪明的主意吗?

【问题讨论】:

  • 我想你应该可以做到:calendar.month_name[month],其中month是代表月份的整数。
  • 谢谢阿洛克。是的,但它在默认语言环境中

标签: python calendar


【解决方案1】:

哈!找到了一种获取本地化日/月名称的简单方法:

>>> import locale
>>> locale.setlocale(locale.LC_ALL, 'de_DE')
'de_DE'
>>> import calendar
>>> calendar.month_name[10]
'Oktober'
>>> calendar.day_name[1]
'Dienstag'

【讨论】:

  • 这只是我答案的精简版。 TimeEncoding 上下文只是临时设置语言环境的一种方式。如果你想永久保存,直接拨打setlocale 就可以了。我猜只有在月/日名称中有 unicode 字符时才需要编码步骤,而德语似乎不是这种情况。
  • 哦,好的。是的,我们有 unicode 字符:>>> calendar.month_name[3] 'M\xc3\xa4rz'
【解决方案2】:

这是来自calendar模块的源代码:

def formatmonthname(self, theyear, themonth, width, withyear=True):
    with TimeEncoding(self.locale) as encoding:
        s = month_name[themonth]
        if encoding is not None:
            s = s.decode(encoding)
        if withyear:
            s = "%s %r" % (s, theyear)
        return s.center(width)

TimeEncodingmonth_name 可以从 calendar 模块导入。这给出了以下方法:

from calendar import TimeEncoding, month_name

def get_month_name(month_no, locale):
    with TimeEncoding(locale) as encoding:
        s = month_name[month_no]
        if encoding is not None:
            s = s.decode(encoding)
        return s

print get_month_name(3, "nb_NO.UTF-8")

对我来说,不需要解码步骤,只需在 TimeEncoding 上下文中打印 month_name[3] 即可打印“mars”,这是挪威语中“march”的意思。

对于工作日,使用day_nameday_abbr dicts 有类似的方法:

from calendar import TimeEncoding, day_name, day_abbr

def get_day_name(day_no, locale, short=False):
    with TimeEncoding(locale) as encoding:
        if short:
            s = day_abbr[day_no]
        else:
            s = day_name[day_no]
        if encoding is not None:
            s = s.decode(encoding)
        return s

【讨论】:

  • 谢谢懒人。嗯,它有效。我希望有一种更简单的方法来实现这一点。我想我也可以导入day_name。我想我会硬编码几个月和几天的名字(需要两行代码)。我们只需要德语,月份/日期名称在未来几年内不太可能发生变化。不过还是谢谢。
  • @Daniel 没问题。即使您并不需要它,但现在下一个需要它的人的答案就在这里。
  • 是的。这太复杂了。我认为有一种简单的方法可以访问这些信息,例如设置具有特定语言环境的日历实例。
  • 在 pyhton3 中 TimeEncoding 被重命名为 different_locale。它的工作方式也略有不同,不再需要 decode 调用。
【解决方案3】:

这是针对 Python 3 更新的 Lauritz 答案的月份部分:

from calendar import month_name, different_locale
def get_month_name(month_no, locale):
    with different_locale(locale):
        return month_name[month_no]

【讨论】:

  • 我收到警告 'different_locale' is not declared in __all__。此外,找不到different_locale 的文档。
  • 这里有语法错误/错字,应该删除最后一个“)”
  • @HåkonT。谢谢,我修好了。
猜你喜欢
  • 1970-01-01
  • 2011-07-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-06
  • 2014-01-18
  • 2011-08-31
  • 1970-01-01
相关资源
最近更新 更多