1 回答
TA贡献1871条经验 获得超13个赞
以下csv是我在评论中建议的使用模块的方法:
import csv
from datetime import datetime, timedelta
import os
today = datetime.today()
cutoff = datetime(year=today.year, month=today.month, day=today.day,
hour=21, minute=45)
max_time_diff = timedelta(hours=24)
input_file = 'date_temp.txt'
save_path = './clips'
complete_name = os.path.join(save_path, 'clips'+today.strftime('%Y-%m-%d')+'.txt')
os.makedirs(save_path, exist_ok=True) # Make sure dest directory exists.
with open(input_file, newline='') as oldfile, \
open(complete_name, 'w', newline='') as newfile:
reader = csv.reader(oldfile)
writer = csv.writer(newfile)
next(reader) # Skip header.
for line in reader:
line_date = datetime.strptime(line[3], "%Y-%m-%d %H:%M:%S")
if cutoff - line_date < max_time_diff:
writer.writerow(line)
print('done')
添加回答
举报