I have been reading several posts (1, 2, 3) about this but I can't make it work yet. I have a (simplified) CSV file like this:
NOMBRE,APELLIDO,ID,NACIMIENTO,FECHAINGRESO,MAILPERSONAL,DEPARTAMENTO
name1,lastname1,123,2000-01-01,2021-03-13,mymail1@example-com,IT
name2,lastname2,456,1999-01-01,2020-01-21,mymail2@example-com,IT
I want to sort it according to the header FECHAINGRESO
, oldest date first, but don't how to do this. I have tried this with python 3.8.5 in Ubuntu 20:
import csv
import os
from datetime import datetime
# With this I read the cvs and print it to check if everything is ok
with open('Empleados.csv', newline='') as csvfile:
spamreader = csv.reader(csvfile, delimiter=',')
for row in spamreader:
print(', '.join(row))
# The next is the code from several attempts where I failed to sort the cvs
with open('Empleados.csv', newline='') as csvfile:
# I wrote 4 because I belive the position 4 in the headers' row is the one with FECHAINGRESO
csvfile = sorted(csvfile, key = lambda row: datetime.strptime(row[4], "%d-%m-%Y"))
print(csvfile)
s = sorted(csvfile, key=lambda x:datetime.strptime(x[4],"%d-%m-%Y"), reverse=True)
print(s)
l = sorted(csvfile, key=lambda x: x[4], reverse=True)
print(l)
sortedlist = sorted(csvfile, key=operator.itemgetter(4), reverse=False)
print(sortedlist)
sortedlist = sorted(csvfile, key=lambda row: row[4], reverse=True)
print(sortedlist)
Basically none of them worked because it reads the row like a string and usually returns this error:
File "/home/Pruebas VSC/prueba_postgresql.py", line 31, in <module>
csvfile = sorted(csvfile, key = lambda row: datetime.strptime(row[4], "%d-%m-%Y"))
File "/home/Pruebas VSC/prueba_postgresql.py", line 31, in <lambda>
csvfile = sorted(csvfile, key = lambda row: datetime.strptime(row[4], "%d-%m-%Y"))
File "/usr/lib/python3.8/_strptime.py", line 568, in _strptime_datetime
tt, fraction, gmtoff_fraction = _strptime(data_string, format)
File "/usr/lib/python3.8/_strptime.py", line 349, in _strptime
raise ValueError("time data %r does not match format %r" %
ValueError: time data 'E' does not match format '%d-%m-%Y'
Some of them don't fail, but they don't sort by the date in that column the CSV.
Everything in the last with-open are codes from other questions I found in google, but I don't understand everything. Hope someone can help me understand how to sort this list. I want to save the CSV file later, but I believe that should be easy with write after it is sorted.