Python 日期格式化 及 schwartzian排序
__author__ = 'root' import datetime import time import copy # 12/Dec/2012:23:59:50 # 12/Sep/2012:23:58:50 # 11/Sep/2012:23:59:50 # 19/Sep/2012:23:58:50 def getTime(dtstr): print dtstr time = datetime.datetime.strptime(dtstr, '%d/%b/%Y:%H:%M:%S') return time.strftime('%Y%m%d%H%M%S') lines = open('datefmt.txt').readlines() lines = [i.strip() for i in lines] line_copy = copy.copy(lines) lines = [getTime(line) for line in lines] lines.sort() for line in lines: print line line_copy.sort() for item in line_copy: print item
12/Dec/2012:23:59:50 12/Sep/2012:23:58:50 11/Sep/2012:23:59:50 19/Sep/2012:23:58:50 20120911235950 20120912235850 20120919235850 20121212235950 11/Sep/2012:23:59:50 12/Dec/2012:23:59:50 12/Sep/2012:23:58:50 19/Sep/2012:23:58:50