在 Python 3 中拼出日期的每个单词
Spell out each word of a date in Python 3
在Python3中,我想把2018-01-01改成"January first, two thousand and eighteen"。我查看了 the Python datetime documentation 中的格式指南
但我没有看到拼写月份日期或拼写年份的格式。是否有一个众所周知的模块可以做到这一点(我是 Python 的新手)?
使用 inflect 模块,如 所述,您可以:
import datetime as dt, inflect
p = inflect.engine()
def date_in_words(x):
a = dt.datetime.strptime(x,'%Y-%m-%d')
return (a.strftime('%B ')+p.number_to_words(p.ordinal(int(a.day)))+', ' +
p.number_to_words(int(a.year)))
date_in_words('2018-03-14')
Out[259]: 'March fourteenth, two thousand and eighteen'
在Python3中,我想把2018-01-01改成"January first, two thousand and eighteen"。我查看了 the Python datetime documentation 中的格式指南 但我没有看到拼写月份日期或拼写年份的格式。是否有一个众所周知的模块可以做到这一点(我是 Python 的新手)?
使用 inflect 模块,如
import datetime as dt, inflect
p = inflect.engine()
def date_in_words(x):
a = dt.datetime.strptime(x,'%Y-%m-%d')
return (a.strftime('%B ')+p.number_to_words(p.ordinal(int(a.day)))+', ' +
p.number_to_words(int(a.year)))
date_in_words('2018-03-14')
Out[259]: 'March fourteenth, two thousand and eighteen'