搜索了一顿,好像还是没明白出错原因
# coding=utf-8
# python2.7
from datetime import datetime
import re
t1= u"2018 年 01 月 1"
t2= u"2018 年 02 月 22"
t3= u"2018 年 3 月 37 日"
def str2digi(strin):
m = re.match(u"(\d+)年(\d+)月(\d+)日?", strin)
yearn = m.group(1)
monthn = m.group(2)
dayn = m.group(3)
if len(m.group(2)) == 1:
monthn = '0'+ monthn
if len(m.group(3)) == 1:
dayn = '0' + dayn
cc = yearn + monthn + dayn
print 'firstdata is ',cc
print [cc]
ee = str(cc)
print [ee]
time = datetime.strptime(ee, "%Y%m%d").date()
return time
print 'time is [%s]' % str2digi(t1)
print 'time is [%s]' % str2digi(t2)
print 'time is [%s]' % str2digi(t3)
firstdata is 20180101
[u'20180101']
['20180101']
time is [2018-01-01]
firstdata is 20180222
[u'20180222']
['20180222']
time is [2018-02-22]
firstdata is 20180337
[u'20180337']
['20180337']
Traceback (most recent call last):
File ".\t2.py", line 40, in <module>
print 'time is [%s]' % str2digi(t3)
File ".\t2.py", line 34, in str2digi
time = datetime.strptime(ee, "%Y%m%d").date()
File "C:\Python27\lib\_strptime.py", line 335, in _strptime
data_string[found.end():])
ValueError: unconverted data remains: 7
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.