import datetime print datetime.datetime.now() # 2016-03-28 17:16:00.812000 a = ‘2016-03-28 17:16:00.812000’ timeArray = time.strptime(a, '%Y-%m-%d %H:%M:%S.%f') ##注意: %f是microseconds的格式化符號。 print timeArray #time.struct_time(tm_year=2016, tm_mon=3, tm_mday=28, tm_hour=17, tm_min=22, tm_sec=38, tm_wday=0, tm_yday=88, tm_isdst=-1)
a = datetime.datetime.now() print a ## a變量不指向一個字符串對象,而是指向一個datetime.datetime類型的對象 # 2016-03-28 17:24:52.571000 timeArray = time.strptime(a, '%Y-%m-%d %H:%M:%S.%f') ## 全部這裏報錯。 time.striptime的第一個參數,應該是字符串對象 Traceback (most recent call last): File "<console>", line 1, in <module> File "C:\Python27\lib\_strptime.py", line 467, in _strptime_time return _strptime(data_string, format)[0] File "C:\Python27\lib\_strptime.py", line 322, in _strptime found = format_regex.match(data_string) TypeError: expected string or buffer