我想做的是:
- Delete all rows where csv date is lower than 25.05.2016 23:59
- Save the file with a different name
我在csv的colA
中有以下数据
WFQVG98765
FI Quality-Value-Growth
Some Random String 1
Datum
13-05-2016 23:59
14-05-2016 23:59
15-05-2016 23:59
16-05-2016 23:59
17-05-2016 23:59
18-05-2016 23:59
19-05-2016 02:03
.
.
.
.
这就是我现在所尝试的
^{pr2}$csvDate = datetime.datetime.strptime(row[0], '%d-%m-%Y %H:%M:%S') 'Error Here : ValueError: time data '' does not match format '%d-%m-%Y %H:%M:%S'
我也尝试了这个错误线
csvDate = datetime.datetime.strptime(row[0], '%d-%m-%Y %H:%M') 'But got the same error
if csvDate<cmpDate:
print (row[0]+'TRUE')
Here how can I delete the row if the condition is true and finally save it with a different name ?
您可以分析每一行以比较日期,并将要保留的行保存在
list
中。然后,您可以将这些行存储到一个新的csv文件中,如果您不再需要旧的csv文件,可以将其删除。在这里有一个截图,可以满足您的要求:
is_date()
给你的是假阳性。在检查日期格式时要更加严格,在将日期字符串加载到datetime
时要更加严格-遵循^{换句话说,不要混合}。在
dateutil.parser.parse()
和{datetime.datetime.strptime
异常表示您正在向row[0]
中的函数传递一个空白字符串。在一旦解决了这个问题,就需要添加代码将可接受的行写入新文件。在
相关问题 更多 >
编程相关推荐