python排序日期按升序排列

时间:2016-07-05 13:59:19

标签: python sorting date

我试图按升序排序这些日期,这些日期记录在文本文件中,然后我的python脚本从文本文件中读取它。

这是我的代码:

#!/usr/bin/python
import sys
import csv 
import re
import datetime
from datetime import date, timedelta

# This script will sync AWS files and load new files into PostgreSQL 

# Run AWS Sync 
program = "C:\\Work\\scripts\\bat\\syncCS.bat"
myoutput = open('C:\\Work\\scripts\\logs\\CSI.txt', 'w')
import subprocess
subprocess.call([program],stdout=myoutput )

# Read log file and load dates in PostgreSQL
file = open('C:\\Work\\scripts\\logs\\CSI.txt', 'r')
data = file.read()

pattern  = re.compile("[\d]{4}-\d{2}-\d{2}")
raw_dates = pattern.findall(data)

l = raw_dates
distinct_l = set(l)


sentence = distinct_l
sent_str = ""
for i in sentence:
    sent_str += str(i) + ","
sent_str = sent_str[:-1]

s = sent_str
dates = s.split(",")
for strdate in reversed(dates):
    newdate = datetime.datetime.strptime(strdate, "%Y-%m-%d").date()
    print(newdate)

output being return is 
2016-07-03
2016-07-04
2016-07-01
2016-06-30
2016-07-02

我正在使用“反向日期”仍然无效,有什么方法可以按升序排序这些日期吗?以下是我想要的输出

desired output return is 
2016-06-30
2016-07-01
2016-07-02
2016-07-03
2016-07-04

0 个答案:

没有答案