【发布时间】:2021-09-07 09:14:13
【问题描述】:
我已经阅读了几篇关于此的帖子(1、2、3),但我还不能让它发挥作用。我有一个(简化的)CSV 文件,如下所示:
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
我想根据标题FECHAINGRESO 对它进行排序,最旧的日期在前,但不知道如何做到这一点。我已经在 Ubuntu 20 中使用 python 3.8.5 尝试过这个:
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)
基本上它们都不起作用,因为它像字符串一样读取行并且通常返回此错误:
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'
其中一些不会失败,但它们不会按 CSV 列中的日期排序。
最后一个 with-open 中的所有内容都是我在 google 中找到的其他问题的代码,但我不明白所有内容。希望有人可以帮助我了解如何对这个列表进行排序。我想稍后保存 CSV 文件,但我相信它在排序后应该很容易写入。
【问题讨论】:
-
你反对使用另一个库,比如
pandas?如果你把它变成pandas格式,很多排序操作都会非常轻松。 -
csvfile不是列表,是文件,所以无法排序。 -
@barny Python 中的文件对象是可迭代的,可以逐行生成文件内容,所以
with open('test.txt') as f: sorted(f)非常好 -
是的,它是一个可迭代的行,而不是读取为 csv 的结果
-
@William 我刚刚在阅读有关 pandas (stackoverflow.com/questions/54911225/…) 的信息,但仍然可以使其工作。让我看看你写的答案
标签: python csv sorting datetime