Reputation: 37
I want to sort the csv file by date but there's this error
ValueError: time data '.' does not match format '%d/%m/%Y'
I'm not able to figure out what I'm doing wrong.
csv file:
T.No,Date,Task,Project,Context,Message,Status
1,12/07/2019,meet @sam and @jack,python,@sam,hello,Incomplete
1,11/07/2019,meet @sam and @jack,python,@jack,hello,Incomplete
3,15/07/2019,meet @sam and @ jack,python,@sam & @,at room 12,Incomplete
4,13/07/2019,meet @sam and @jack,python,@sam & @jack,at room 12,Incomplete
5,15/07/2019,meet sam,python,,at room 12,Incomplete
def sort():
data = open("csv.csv", 'r')
stdata = sorted(data, key=lambda row: datetime.strptime(row[1], "%d/%m/%Y"))
with open("csv.csv", 'w') as f1:
writer = csv.writer(f1)
for eachline in stdata:
writer.writerows(eachline)
Upvotes: 1
Views: 1551
Reputation: 8270
It is easier to use pandas.DataFrame for reading CSV and sorting operations:
import pandas as pd
df = pd.read_csv('csv.csv', index_col=False)
df['Date'] = pd.to_datetime(df['Date']).dt.strftime('%d/%m/%Y')
df.sort_values('Date', inplace=True)
df.to_csv('result.csv', index=False)
Output:
T.No,Date,Task,Project,Context,Message,Status
1,07/11/2019,meet @sam and @jack,python,@jack,hello,Incomplete
1,07/12/2019,meet @sam and @jack,python,@sam,hello,Incomplete
4,13/07/2019,meet @sam and @jack,python,@sam & @jack,at room 12,Incomplete
3,15/07/2019,meet @sam and @ jack,python,@sam & @,at room 12,Incomplete
5,15/07/2019,meet sam,python,,at room 12,Incomplete
Upvotes: 3
Reputation: 1150
You could use headers = next(reader)
to remove the first row not including a date.
from datetime import datetime
import csv
with open('data.csv', newline='') as csv_file:
reader = csv.reader(csv_file)
headers = next(reader) #removing the first row from your reader
sorted_reader = sorted(reader, key = lambda row: datetime.strptime(str(row[1]), "%d/%m/%Y"))
for row in sorted_reader:
print(row)
Upvotes: 2