我所試圖做的是:ValueError異常:實時數據 '' 不匹配格式 '%D-%間%Y%H:%M:%S'
- Delete all rows where csv date is lower than 25.05.2016 23:59
- Save the file with a different name
我有以下在山坳的CSV數據A
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
.
.
.
.
這是我現在試圖
import csv
import datetime
from dateutil.parser import parse
def is_date(string):
try:
parse(string)
return True
except ValueError:
return False
'''
1. Delete all rows where csv date is lower than 25.05.2016 23:59
2. Save the file with a different name
'''
cmpDate = datetime.datetime.strptime('25.05.2016 23:59:00', '%d.%m.%Y %H:%M:%S')
with open('WF.csv', 'r') as csvfile:
csvReader = csv.reader(csvfile, delimiter=',')
for row in csvReader:
print (row[0])
if is_date(row[0]) and not row[0].strip(' '):
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 ?
謝謝,但我不這樣做爲什麼它留下兩行之間的空行,這實際上並不工作,即在output.csv文件中我有所有的行輸入文件:/ – newguy
@newguy輸入csv是否使用','作爲分隔符?它在第一行有日期嗎?你可以把你的csv文件頭幾行添加一個頭文件嗎? – kardaj
這個問題有確切的文字是什麼在csv列A和是','是我的csv的分隔符 – newguy