如果日期早于24小时,则从文本文件中删除行

时间:2019-03-28 21:39:58

标签: python python-3.x

我有一个名为temp.txt的文本文件,如果日期每天从21:45 pm开始超过24小时,我想删除其中的所有行。我做了很多谷歌搜索,却找不到任何答案。文本文件采用这种格式,没有标题:

http://clipsexample1.com,clips1,clipexample123,2019-03-28 17:14:14
http://clipsexample12com,clips2,clipexample234,2019-03-27 18:56:20

无论如何,如果存在时间超过24小时,我可以删除整行(示例中的第二个剪辑)

编辑:我尝试使用此代码,但这只是删除今天的日期,我如何才能删除今天的24小时?

save_path = 'clips/'
completeName = os.path.join(save_path, 'clips'+str(today)+'.txt')
good_dates = [str(today)]
with open('temp.txt') as oldfile, open(completeName, 'w') as newfile:
    for line in oldfile:
        if any(good_date in line for good_date in good_dates):
            newfile.write(line)

编辑30/03/2019:这是我的完整代码,以尝试了解时间戳记字段的创建方式:

#change UNIX to standard date format
def get_date(created_utc):
    return dt.datetime.fromtimestamp(created_utc)
_timestamp = topics_data["created_utc"].apply(get_date)
topics_data = topics_data.assign(timestamp = _timestamp)
timestamp = _timestamp
print(timestamp)

#remove UNIX data column
topics_data.drop('created_utc', axis=1, inplace=True)

#export clips to temp.txt
topics_data.to_csv('temp.txt', header=True, index=False)

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 = '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)

    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)

当我打印时间戳字段时,这是我得到的结果:

01   2019-03-29 01:22:09
02   2019-03-29 02:42:21
03   2019-03-28 17:14:14
04   2019-03-29 06:06:18
Name: created_utc, dtype: datetime64[ns]

我仍然遇到的错误是:

ValueError: time data 'timestamp' does not match format '%Y-%m-%d %H:%M:%S'

即使日期时间以这种格式打印?

1 个答案:

答案 0 :(得分:1)

以下是我在评论中建议的使用click模块的方法:

csv