按日期对csv文件进行排序,然后将它们重写在同一文件中

时间:2019-07-15 06:17:39

标签: python csv sorting

我想按日期对csv文件进行排序,但是出现此错误

ValueError: time data '.' does not match format '%d/%m/%Y'

我无法弄清楚我在做什么错。

csv文件:

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)

3 个答案:

答案 0 :(得分:2)

您可以使用headers = next(reader)删除不包括日期的第一行。

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)

答案 1 :(得分:2)

使用pandas.DataFrame来读取CSV和进行排序操作更加容易:

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)

输出:

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

答案 2 :(得分:0)

这对我有用:

datetime.strptime("2009/10/2", "%Y/%m/%d")