检查文本文件中的日期是否过期
Checking if date is overdue from text file
我有一个包含用户和截止日期的文本文件。我希望能够读取用户最初输入的截止日期,并查看该日期与当前日期相比是否过期。任何帮助将不胜感激。
文本文件:
User: Tommy
Gender: Male
Date due: 2020-02-18
我目前的代码是:
import datetime
data = open("duedate.txt", "r")
d = datetime.datetime.today()
line = data.readlines(3)
if line > d
print("This date is overdue ")
试试这个:
import datetime
today = datetime.datetime.today()
with open("duedate.txt", "r") as f:
for line in f:
if not line.startswith('Date due'): continue
field, value = line.split(':')
if field == 'Date due':
if datetime.datetime.strptime(value.strip(), '%Y-%m-%d') < today:
print("Overdue")
我有一个包含用户和截止日期的文本文件。我希望能够读取用户最初输入的截止日期,并查看该日期与当前日期相比是否过期。任何帮助将不胜感激。
文本文件:
User: Tommy
Gender: Male
Date due: 2020-02-18
我目前的代码是:
import datetime
data = open("duedate.txt", "r")
d = datetime.datetime.today()
line = data.readlines(3)
if line > d
print("This date is overdue ")
试试这个:
import datetime
today = datetime.datetime.today()
with open("duedate.txt", "r") as f:
for line in f:
if not line.startswith('Date due'): continue
field, value = line.split(':')
if field == 'Date due':
if datetime.datetime.strptime(value.strip(), '%Y-%m-%d') < today:
print("Overdue")