从用户输入中解析 date/time 有哪些方法?
What are some ways to parse date/time from user input?
class Date:
def __init__(self, digits): #digits='10/20/21'
self.month = digits[:2] #'10'
self.day = digits[3:5] #'20'
self.year = digits[6:8] #'21'
def __str__(self):
return f"Dated this {self.day} day of {self.month}, 20{self.year}"
def checkday(date): #add 'st', 'nd', 'rd', or 'th' to day
if int(date.day) == 1 or int(date.day) == 21 or int(date.day) == 31:
date.day += 'st'
elif int(date.day) == 2 or int(date.day) == 22:
date.day += 'nd'
elif int(date.day) == 3 or int(date.day) == 23:
date.day += 'rd'
else:
date.day += 'th'
def checkmonth(date): #get name of month
date.month = monthdic[date.month]
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'June', 'Jul', 'Aug', 'Sep', 'Oct','Nov', 'Dec']
monthdic = {str(i): month for i, month in zip(range(1,13), months)}
date = Date(input("Enter date (mm/dd/yy):\t"))
checkday(date)
checkmonth(date)
print(date)
几个错误归结为一个我没有想到的问题:
如果是一月:1/12/14
将不起作用,因为 self.month
是 1/12/14[:2]
。
Enter date (mm/dd/yy): 1/12/14
Traceback (most recent call last):
File "date.py", line 38, in <module>
checkday(date)
File "date.py", line 14, in checkday
if int(date.day) == 1 or int(date.day) == 21 or int(date.day) == 31:
ValueError: invalid literal for int() with base 10: '2/'
如果我求助于 01/12/14
,这也不起作用,因为 01
是 '01'
而 monthdic['01']
不存在:
Enter date (mm/dd/yy): 01/12/14
Traceback (most recent call last):
File "date.py", line 39, in <module>
checkmonth(date)
File "date.py", line 28, in checkmonth
date.month = monthdic[date.month]
KeyError: '01'
显然
def __init__(self, digits):
self.month = digits[:2]
self.day = digits[3:5]
self.year = digits[6:8]
不是最好的方法,有哪些好的方法(除了正则表达式)?
还有一件事:在 __init__
中调用 checkdate()
和 checkmonth
合适吗?
最好的方法是拆分法。您可以根据 '/'
拆分正在输入的文本。所以,像 1/12/14
这样的东西会变成 [1, 12, 14]
。剩下的就简单了。
class Date:
def __init__(self, digits):
split = digits.split('/')
self.month = split[0]
self.day = split[1]
self.year = split[2]
def __str__(self):
return f"Dated this {self.day} day of {self.month}, {self.year}"
作为@MuhameedJaseem :split = digits.split('/')
并与 split
一起工作。还按照许多建议改进了代码。代码现在更优雅了。
class Date:
def __init__(self, digits):
self.month, self.day, self.year = digits.split('/')
def __str__(self):
return f"Dated this {self.day} day of {self.month}, {self.year}"
def checkday(date):
if date.day[-1] == '1' and date.day[0] != '1':
date.day += 'st'
elif date.day[-1] == '2' and date.day[0] != '1':
date.day += 'nd'
elif date.day[-1] == '3' and date.day[0] != '1':
date.day += 'rd'
else:
date.day += 'th'
def checkmonth(date):
date.month = monthdic[date.month]
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'June', 'Jul', 'Aug', 'Sep', 'Oct','Nov', 'Dec']
monthdic = {str(i): month for i, month in zip(range(1,13), months)}
date = Date(input("Enter date (m/d/y):\t"))
checkday(date)
checkmonth(date)
class Date:
def __init__(self, digits): #digits='10/20/21'
self.month = digits[:2] #'10'
self.day = digits[3:5] #'20'
self.year = digits[6:8] #'21'
def __str__(self):
return f"Dated this {self.day} day of {self.month}, 20{self.year}"
def checkday(date): #add 'st', 'nd', 'rd', or 'th' to day
if int(date.day) == 1 or int(date.day) == 21 or int(date.day) == 31:
date.day += 'st'
elif int(date.day) == 2 or int(date.day) == 22:
date.day += 'nd'
elif int(date.day) == 3 or int(date.day) == 23:
date.day += 'rd'
else:
date.day += 'th'
def checkmonth(date): #get name of month
date.month = monthdic[date.month]
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'June', 'Jul', 'Aug', 'Sep', 'Oct','Nov', 'Dec']
monthdic = {str(i): month for i, month in zip(range(1,13), months)}
date = Date(input("Enter date (mm/dd/yy):\t"))
checkday(date)
checkmonth(date)
print(date)
几个错误归结为一个我没有想到的问题:
如果是一月:1/12/14
将不起作用,因为 self.month
是 1/12/14[:2]
。
Enter date (mm/dd/yy): 1/12/14
Traceback (most recent call last):
File "date.py", line 38, in <module>
checkday(date)
File "date.py", line 14, in checkday
if int(date.day) == 1 or int(date.day) == 21 or int(date.day) == 31:
ValueError: invalid literal for int() with base 10: '2/'
如果我求助于 01/12/14
,这也不起作用,因为 01
是 '01'
而 monthdic['01']
不存在:
Enter date (mm/dd/yy): 01/12/14
Traceback (most recent call last):
File "date.py", line 39, in <module>
checkmonth(date)
File "date.py", line 28, in checkmonth
date.month = monthdic[date.month]
KeyError: '01'
显然
def __init__(self, digits):
self.month = digits[:2]
self.day = digits[3:5]
self.year = digits[6:8]
不是最好的方法,有哪些好的方法(除了正则表达式)?
还有一件事:在 __init__
中调用 checkdate()
和 checkmonth
合适吗?
最好的方法是拆分法。您可以根据 '/'
拆分正在输入的文本。所以,像 1/12/14
这样的东西会变成 [1, 12, 14]
。剩下的就简单了。
class Date:
def __init__(self, digits):
split = digits.split('/')
self.month = split[0]
self.day = split[1]
self.year = split[2]
def __str__(self):
return f"Dated this {self.day} day of {self.month}, {self.year}"
作为@MuhameedJaseem split = digits.split('/')
并与 split
一起工作。还按照许多建议改进了代码。代码现在更优雅了。
class Date:
def __init__(self, digits):
self.month, self.day, self.year = digits.split('/')
def __str__(self):
return f"Dated this {self.day} day of {self.month}, {self.year}"
def checkday(date):
if date.day[-1] == '1' and date.day[0] != '1':
date.day += 'st'
elif date.day[-1] == '2' and date.day[0] != '1':
date.day += 'nd'
elif date.day[-1] == '3' and date.day[0] != '1':
date.day += 'rd'
else:
date.day += 'th'
def checkmonth(date):
date.month = monthdic[date.month]
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'June', 'Jul', 'Aug', 'Sep', 'Oct','Nov', 'Dec']
monthdic = {str(i): month for i, month in zip(range(1,13), months)}
date = Date(input("Enter date (m/d/y):\t"))
checkday(date)
checkmonth(date)