strptime()方法分析表示根據格式的時間字符串。返回值是一個struct_time所返回gmtime()或localtime()。
格式參數使用相同的指令使用strftime();它默認為“%a %b %d %H:%M:%S %Y”相匹配的ctime()所返回的格式。
如果字符串不能按格式進行解析,或者如果它具有解析后多余的數據,ValueError被掛起。
語法
以下是strptime()方法的語法:
time.strptime(string[, format])
參數
string -- 這是其中將根據給定的格式解析字符串格式的時間。 format -- 這是將用于解析該給定的字符串的指令。下面的指令可以嵌入格式字符串:
指令
返回值
這個返回struct_time所返回gmtime()或localtime()的值。
例子
下面的例子顯示 strptime()方法的使用。
#!/usr/bin/pythonimport timestruct_time = time.strptime("30 Nov 00", "%d %b %y")print "returned tuple: %s " % struct_timeWhen we run above program, it produces following result:returned tuple: (2000, 11, 30, 0, 0, 0, 3, 335, -1)            
新聞熱點
疑難解答