2017-06-28 67 views
2

我正在以2017-01-04的形式從csv閱讀日期到python。我試圖處理日期像Python中的時間數據格式

from datetime import datetime  
day1 = datetime.strptime(date1, date_format) 

,我使用date_format = "%y-%m-%d"

然而,當我處理這些數據,我得到的錯誤:

time data '2017-01-04' does not match format '%y-%m-%d'

什麼它應該是什麼?

+3

它是''%Y-%m-%d「'大寫' Y'爲4位數字,請參閱[文檔](https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior) – EdChum

回答

0

喜歡的東西:

from datetime import datetime 
day1 = datetime.utcnow().strptime('2017-01-04', '%Y-%m-%d') 
4

Y - Year with century as a decimal number.

y - Year without century as a zero-padded decimal number.

因此,你應該使用上Y.

有關格式的更多信息,你可以看到這一切在here

0

You can run it-This code run successfully and print-2017-01-04 00:00:00

 from datetime import datetime 
    day1 = datetime.utcnow().strptime('2017-01-04', '%Y-%m-%d') 
    print(day1)