對象我這是在給定的Excel中的日期:dd mmm yy
格式即轉換日期字符串到日期在python
29 Jun 18
如何這個字符串轉換成日期對象?
我得到的錯誤:
time data '13 Jul 18' does not match format '%d %m %Y'
當我嘗試
datetime.strptime(input, '%d %m %Y')
應該怎樣正確的日期格式是什麼?
對象我這是在給定的Excel中的日期:dd mmm yy
格式即轉換日期字符串到日期在python
29 Jun 18
如何這個字符串轉換成日期對象?
我得到的錯誤:
time data '13 Jul 18' does not match format '%d %m %Y'
當我嘗試
datetime.strptime(input, '%d %m %Y')
應該怎樣正確的日期格式是什麼?
您可以使用python datetime模塊,也可以使用dateutil解析器將字符串日期解析爲有效的datetime對象。我會去dateutil parser
,因爲我不必定義字符串格式。下面是一個例子
from dateutil.parser import parse
dt = parse("Thu Sep 25 10:36:28 BRST 2003")
記得pip install python-dateutil
安裝dateutil由於您EXCELL全年只有兩個數字(即18,而不是2018),你需要在你要使用的格式%y
代替%Y
串:
datetime.strptime(input, '%d %b %y')
例如:
datetime.strptime('13 Jul 18', '%d %b %y')
個
結果與:
datetime.datetime(2018, 7, 13, 0, 0)
有關日期/時間字符串格式的詳細信息,請參閱this page。
您可以使用strptime的跟隨模式:
datetime.strptime(date_string, format)
例如:
from datetime import datetime
dt = datetime.strptime('19 Jul 2017', '%d %b %y')
希望這有助於:)
你將不得不import datetime
和import xlrd
使用xlrd
打開Excel工作簿作爲
book = xlrd.open_workbook("Excel.xlsx") sheet = book.sheet_by_name("Worksheet")
使用
obj = datetime.datetime(*xlrd.xldate_as_tuple(sheet.cell(row,column).value, book.datemode))
from datetime import datetime
datetime.strptime("29 Jun 18", "%d %b %y").date()
在這裏,你得到一個datetime.date對象這個轉換,我不知道這對你是否足夠好。我建議您訪問documentation on the module
你的格式不正確:%B是區域設置的短月份和%y是兩位數年份
import time
time.strptime('13 Jul 18', '%d %b %y')
time.struct_time(tm_year=2018, tm_mon=7, tm_mday=13, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=4, tm_yday=194, tm_isdst=-1)
那你試試這麼遠嗎? – Humbalan