Python中从数字到单词的日期转换

Python中从数字到单词的日期转换,python,dictionary,date-conversion,Python,Dictionary,Date Conversion,我用python编写了一个程序(我还是个初学者),它使用字典将日期从数字转换为单词。程序是这样的: dictionary_1 = { 1:'first', 2:'second'...} dictionary_2 = { 1:'January', 2:'February',...} import re dictionary_1 = { 1:'first', 2:'second'} dictionary_2 = { 1:'January', 2:'February'} dictionary_3

我用python编写了一个程序(我还是个初学者),它使用字典将日期从数字转换为单词。程序是这样的:

dictionary_1 = { 1:'first', 2:'second'...}
dictionary_2 = { 1:'January', 2:'February',...}
import re

dictionary_1 = { 1:'first', 2:'second'}
dictionary_2 = { 1:'January', 2:'February'}
dictionary_3 = { 1996:'asd', 1995:'asd1'}

input1 = raw_input("Enter date:")
lista = re.split(r'[.\/-]', input1)
print "lista: ", lista
day = lista[0]
month = lista[1]
year = lista[2]
everything_ok = False
if dictionary_1.get(int(day)) != None:
   day_print = dictionary_1.get(int(day))
   everything_ok = True
else:
   print "There is no such day"
if dictionary_2.get(int(month)) != None:
   month_print = dictionary_2.get(int(month))
   everything_ok = True
else:
   print "There is no such month"
   everything_ok = False
if dictionary_3.get(int(year)) != None:
   year_print = dictionary_3.get(int(year))
   everything_ok = True
else:
   print "There is no such year"
   everything_ok = False
if everything_ok == True:
   print "Date: ", day_print, "/", month_print, "/", year_print #or whatever format
else:
   pass
还有另外三个,几十个,几百个,几千个

  • 2项功能,一项使用1000年
  • 验证日期是否有效的算法
我主要有:

a_random_date = raw_input("Enter a date: ") 
(我为特殊字符选择了原始输入。在数字之间,例如:2014年11月21日或2014年11月21日或2014年11月21日,仅这三个数字)在验证日期是否有效后,我不知道也没有找到如何调用字典将日期转换为单词,当我运行程序时,我希望在输出处输入,例如,如果我键入1/1/2015:first/一月/2015。 我想把这个程序应用到一个文本文档中去寻找日期,如果可能的话,把它们从数字转换成文字。
谢谢大家!

最终您将需要re模块。学习编写可以搜索特定格式字符串的正则表达式。下面是一些代码示例:

with open("mydocument.txt") as f:
    contents = f.read()

fi = re.finditer(r"\d{1,2}-\d{1,2}-\d{4}", contents)
这将查找由1或2个数字后跟连字符、1或2个数字后跟连字符、4个数字组成的所有字符串。然后,将每个字符串输入到
datetime.strtime
;它将解析您的“日期”字符串,并根据您指定的格式决定它是否有效


玩得开心

您可以在列表中拆分该日期,然后检查字典中是否有该日期,如下所示:

dictionary_1 = { 1:'first', 2:'second'...}
dictionary_2 = { 1:'January', 2:'February',...}
import re

dictionary_1 = { 1:'first', 2:'second'}
dictionary_2 = { 1:'January', 2:'February'}
dictionary_3 = { 1996:'asd', 1995:'asd1'}

input1 = raw_input("Enter date:")
lista = re.split(r'[.\/-]', input1)
print "lista: ", lista
day = lista[0]
month = lista[1]
year = lista[2]
everything_ok = False
if dictionary_1.get(int(day)) != None:
   day_print = dictionary_1.get(int(day))
   everything_ok = True
else:
   print "There is no such day"
if dictionary_2.get(int(month)) != None:
   month_print = dictionary_2.get(int(month))
   everything_ok = True
else:
   print "There is no such month"
   everything_ok = False
if dictionary_3.get(int(year)) != None:
   year_print = dictionary_3.get(int(year))
   everything_ok = True
else:
   print "There is no such year"
   everything_ok = False
if everything_ok == True:
   print "Date: ", day_print, "/", month_print, "/", year_print #or whatever format
else:
   pass
这是输出:

Enter date:1/2/1996
Date:  first / February / asd

我希望这对您有所帮助。

如果您这样做是为了学习,那没关系,但您应该知道,有一个标准的库函数用于此:。@nickyd没问题!顺便说一句,请点击正确答案附近的“检查”按钮,将答案标记为正确;)当我实现这个部分时,我收到了一条错误消息:Traceback(最近一次调用):File“/home/nick/data.py”,第142行,在main()文件“/home/nickdata.py”,第110行,在main month=list[1]indexer中:list index超出范围我输入一个带有原始输入的日期,它验证这是否是一个有效的日期,在我添加了你建议的代码后,我得到了上面的错误,我该怎么办?我编辑了我的答案,现在它工作了。很抱歉,我没有检查我的代码,因为我很匆忙。。。再次抱歉@尼克