将日期转换成python中的自然语言

将日期转换成python中的自然语言

本文介绍了将日期转换成python中的自然语言?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何将以下日期转换为自然语言,包括python中的时区?



input: / p>

 2012-09-27T02:00:00Z

预期输出:

  2012年9月26日星期三山区时间

提前感谢!



注意编辑:
到目前为止,我尝试过,虽然它不会处理非常复杂的日期时间字符串。



解决方案:



感谢所有的信息。我最终解析了原来的字符串,并使用了这样的pitz和strftime:

  my_date ='2012-09-27T02:00: 00Z'
utc_date_object = datetime(int(my_date [0:4]),int(my_date [5:7]),int(my_date [8:10]),int(my_date [11:13]), int(my_date [14:16]),int(my_date [17:19]),0,pytz.utc)
mt_date_object = utc_date_object.replace(tzinfo = pytz.utc).astimezone(pytz.timezone('美元/山'))
natural_date = mt_date_object.strftime(%A,%Y%%Y)

输出:

 '2012年9月26日星期三'


解决方案

提供了全功能的。



您还需要解析正确的时区的日期时间字符串。



它可以根据语言环境格式化日期和时间: / p>

 >>> from datetime import date,datetime,time 
>>>>来自babel.dates import format_date,format_datetime,format_time
>>> d = date(2007,4,1)
>>> format_date(d,locale ='en')
u'Apr 1,2007'
>>> format_date(d,locale ='de_DE')
u'01.04.2007'

让我们详细说明格式。这包括格式化时区。



将解析器和格式化程序放在一起:

 >>> dt = iso8601.parse_date(2012-08-25T02:00:00Z)
>>> format_date(dt,MMMM dd,yyyy,locale ='en')+'at'+ format_time(dt,HH:mm V)
u'August 25,2012 at 02:00 World )时间'

Ordinals('1st','2nd'等)有点难做国际上,Babel使用的不包括这些模式。 / p>

如果您必须在日期格式中具有序号(可能是因为您只希望以英文输出),则必须创建你自己:

 >>> suffix =('st'if dt.day in [1,21,31] 
... else'nd'if dt.day in [2,22]
... else'rd'如果dt.day在[3,23]
... else'th')
>>>在{time}'格式(
... date = format_date(dt,MMMM dd,locale ='en'),
的'{date} {suffix},{year} .. suffix = suffix,year = dt.year,
... time = format_time(dt,HH:mm V))
u'August 25th,2012 at 02:00 World(GMT )时间'


I would like to know how to convert the following date to natural language, including time zone in python?

input:

"'2012-09-27T02:00:00Z'"

expected output:

Wednesday, September 26 of 2012 Mountain Time

Thanks in advance!

Note Edit:So far I tried django humanize, although it doesn't handle very well complex date-time strings.

Solution:

Thanks for all the information. I ended up parsing the original string and using pitz and strftime like this:

    my_date = '2012-09-27T02:00:00Z'
    utc_date_object = datetime(int(my_date[0:4]), int(my_date[5:7]), int(my_date[8:10]),int(my_date[11:13]),int(my_date[14:16]),int(my_date[17:19]),0,pytz.utc)
    mt_date_object = utc_date_object.replace(tzinfo=pytz.utc).astimezone(pytz.timezone('US/Mountain'))
    natural_date = mt_date_object.strftime("%A, %B %d of %Y")

Output:

'Wednesday, September 26 of 2012'
解决方案

The Babel project offers a full-featured date and time localization library.

You'll also need the iso8601 module to parse a date-time string with a timezone correctly.

It either formats dates and times based on locale:

>>> from datetime import date, datetime, time
>>> from babel.dates import format_date, format_datetime, format_time
>>> d = date(2007, 4, 1)
>>> format_date(d, locale='en')
u'Apr 1, 2007'
>>> format_date(d, locale='de_DE')
u'01.04.2007'

or it let's you specify the format in detail. This includes formatting the timezone.

Putting the parser and the formatter together:

>>> dt = iso8601.parse_date("2012-08-25T02:00:00Z")
>>> format_date(dt, "MMMM dd, yyyy", locale='en') + ' at ' + format_time(dt, "HH:mm V")
u'August 25, 2012 at 02:00 World (GMT) Time'

Ordinals ('1st', '2nd', etc.) are a little harder to do internationally, and the LDML format used by Babel doesn't include a pattern for these.

If you must have an ordinal in your date formatting (perhaps because you only expect to output in English), you'll have to create those yourself:

>>> suffix = ('st' if dt.day in [1,21,31]
...                else 'nd' if dt.day in [2, 22]
...                else 'rd' if dt.day in [3, 23]
...                else 'th')
>>> u'{date}{suffix}, {year} at {time}'.format(
...     date=format_date(dt, "MMMM dd", locale='en'),
...     suffix=suffix, year=dt.year,
...     time=format_time(dt, "HH:mm V"))
u'August 25th, 2012 at 02:00 World (GMT) Time'

这篇关于将日期转换成python中的自然语言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 08:22