我正试图想出一种以最好的pythonic方式实现这一目标的方法。现在,我能想到的唯一方法是暴力破解它。
用户以下列方式之一输入日期(通过命令行)(例如./mypy.py date ='20110909.00 23')
date='20110909'
date='20110909.00 23'
date='20110909.00 20110909.23'
所有三个示例应该具有相同的结果,如果它填充列表(我可以排序)无关紧要,例如
['20110909.00', '20110909.23]
甚至两个已排序的单独变量,但在所有情况下都是YYYYMMDD.HH,并且需要确保它确实是日期而不是文本。
有什么想法吗?
谢谢。
+++++ EDIT +++++ 在插上这个之后,我想我需要先做很多日期检查/操作。这一切似乎都很有效。除了最后,我通过日期验证运行列表,每次都失败 - 即使它应该通过。
(我发布它) ./test.py date ='20110909.00 23'
(或日期的任何变化 - 即日期= '20 22'或日期='20110909'或日期='20110909.00 23'等)
import sys, re, time, datetime
now = datetime.datetime.now()
tempdate=[]
strfirstdate=None
strtempdate=None
temparg2 = sys.argv
del temparg2[0]
tempdate = temparg2[0].replace('date=','')
date = tempdate.split(' ');
tempdate=[]
date.sort(key=len, reverse=True)
result = None
# If no date is passed then create list according to [YYMMDD.HH, YYMMDD.HH]
if date[0] == 'None':
tempdate.extend([now.strftime('%Y%m%d.00'), now.strftime('%Y%m%d.%H')])
# If length of date list is 1 than see if it is YYMMDD only or HH only, and create list according to [YYMMDD.HH, YYMMDD.HH]
elif len(date) == 1:
if len(date[0]) == 8:
tempdate.extend([ date[0] + '.00', date[0] + '.23'])
elif len(date[0]) == 2:
tempdate.extend([now.strftime('%Y%m%d') + '.' + date[0], now.strftime('%Y%m%d') + '.' + date[0]])
else:
tempdate.extend([date[0], date[0]])
# iterate through list, see if value is YYMMDD only or HH only or YYYYMMDD.HH, and create list accoring to [YYYYMMDD.HH, YYYYMMDD.HH] - maximum of 2 values
else:
for _ in range(2):
if len(date[_]) == 8:
strfirstdate = date[0]
tempdate.append([ date[_] + '.00'])
elif len(date[_]) == 2:
if _ == 0: # both values passed could be hours only
tempdate.append(now.strftime('%Y%m%d') + '.' + date[_])
else: # we must be at the 2nd value passed.
if strfirstdate == None:
tempdate.append(now.strftime('%Y%m%d') + '.' + date[_])
else:
tempdate.append(strfirstdate + '.' + date [_])
else:
strfirstdate = date[0][:8]
tempdate.append(date[_])
tempdate.sort()
for s in tempdate:
try:
result = datetime.datetime.strptime(s, '%Y%m%d.%H')
except:
pass
if result is None:
print 'Malformed date.'
else:
print 'Date is fine.'
print tempdate
++++编辑2 ++++ 如果我删除底部(在tempdate.sort()之后)并用它替换它。
strfirstdate = re.compile(r'([0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]+\.[0-9][0-9])')
for s in tempdate:
if re.match(strfirstdate, s):
result = "validated"
else:
print "#####################"
print "#####################"
print "## error in date ##"
print "#####################"
print "#####################"
exit
它将适当验证。
这整个方法似乎并不是非常pythonic。
答案 0 :(得分:7)
您可以使用try...except
创建一个掩码并对其进行解析,以确定日期字符串是否与多个掩码中的一个匹配。我有一个项目的代码,所以我稍微修改了它:
from time import mktime, strptime
from datetime import datetime
date = '20110909.00 20110909.23'.split(' ')[0]
result = None
for format in ['%Y%m%d', '%Y%m%d.%H']:
try:
result = datetime.strptime(date, format)
except:
pass
if result is None:
print 'Malformed date.'
else:
print 'Date is fine.'
答案 1 :(得分:1)
当我尝试在我自己的解析中使用try..except代码示例时,我发现了一些问题,所以这里是我添加了修复的版本,我还解决了仅处理小时部分的问题:
from datetime import datetime
dates = ['20110909.00','20110909.23','13','20111212','20113131']
def dateTest(date):
dateOk = False
for format in ['%Y%m%d', '%Y%m%d.%H', '%H']:
try:
result = datetime.strptime(date, format)
dateOk = (date == result.strftime(format)) # this makes sure the parsed date matches the original string
if format == '%H': # this handles the hour only case
date = '%s.%s' % (datetime.now().strftime('%Y%m%d'), date)
except:
pass
if dateOk:
print 'Date is fine.'
else:
print 'Malformed date.'
return date
for date in dates:
print date
print dateTest(date)
print ''
答案 2 :(得分:0)
查看time模块。具体来说,请参阅time.strptime()函数。
在时间值和日期时间对象之间也可以轻松转换。
答案 3 :(得分:0)
这对你有帮助吗? :
from datetime import datetime
import re
reg = re.compile('(\d{4})(\d\d)(\d\d)'
'(?:\.(\d\d)(\d\d)?(\d\d)? *'
'(?:(\d{4})(\d\d)(\d\d)\.)?(\d\d)(\d\d)?(\d\d)? *)?')
for x in ('20110909',
'20110909.00 23',
'20110909.00 74',
'20110909.00 20110909.23',
'20110909.00 19980412.23',
'20110909.08 20110909.23',
'20110935.08 20110909.23',
'20110909.08 19970609.51'):
print x
gr = reg.match(x).groups('000')
try:
x1 = datetime(*map(int,gr[0:6]))
if gr[6]=='000':
if gr[9]=='000':
x2 = x1
else:
y = map(int,gr[0:3] + gr[9:12])
try:
x2 = datetime(*y)
except:
x2 = "The second part isn't in range(0,25)"
else:
y = map(int,gr[6:12])
try:
x2 = datetime(*y)
except:
x2 = "The second part doesn't represent a real date"
except:
x1 = "The first part dosen't represent a real date"
x2 = '--'
print [str(x1),str(x2)],'\n'
结果
20110909
['2011-09-09 00:00:00', '2011-09-09 00:00:00']
20110909.00 23
['2011-09-09 00:00:00', '2011-09-09 23:00:00']
20110909.00 74
['2011-09-09 00:00:00', "The hour in the second part isn't in range(0,25)"]
20110909.00 20110909.23
['2011-09-09 00:00:00', '2011-09-09 23:00:00']
20110909.00 19980412.23
['2011-09-09 00:00:00', '1998-04-12 23:00:00']
20110909.08 20110909.23
['2011-09-09 08:00:00', '2011-09-09 23:00:00']
20110935.08 20110909.23
["The first part dosen't represent a real date", '--']
20110909.08 19970609.51
['2011-09-09 08:00:00', "The second part doesn't represent a real date"]
请注意,对于无
的每个组,groups('000')
将无替换为'000'