在Python 3中,我想将2018-01-01更改为“2018年一月一日”。我查看了格式指南Python 日期时间文档 https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior但我没有看到拼写出月份中的某一天或拼写出年份的格式。有没有一个众所周知的模块可以做到这一点(我是Python新手)?
使用 inflect 模块,如评论所述@拉克谢·加尔格 https://stackoverflow.com/questions/52087403/spell-out-each-word-of-a-date-in-python-3#comment91125190_52087403你可以做:
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'
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)