0

I am trying to sort these dates by ascending order , these dates are being logged in text file and then my python scripts read it from text file.

here is my code:

#!/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

I am using "reversed dates" by still it does not work , is there any way I can sort these dates by ascending order? Below is my desired output

desired output return is 
2016-06-30
2016-07-01
2016-07-02
2016-07-03
2016-07-04
user1570210
  • 1,169
  • 12
  • 26
  • 37

0 Answers0