简体   繁体   中英

Sorting Timestamps inside a CSV file with Python

I'm trying to sort the content of a csv file by the given timestamps but it just doesn't seem to work for me. They are given in such a way:

2021-04-16 12:59:26+02:00

My current code:

from datetime import datetime
import csv
from csv import DictReader

with open('List_32_Data_New.csv', 'r') as read_obj:
    csv_dict_reader = DictReader(read_obj)

    csv_dict_reader = sorted(csv_dict_reader, key = lambda row: datetime.strptime(row['Timestamp'], "%Y-%m-%d %H:%M:%S%z"))

    writer = csv.writer(open("Sorted.csv", 'w'))

    for row in csv_dict_reader:
        writer.writerow(row)

However it always throws the error: time data '2021-04-16 12:59:26+02:00' does not match format '%Y-%m-%d %H:%M:%S%z'

I tried already an online compiler at apparently it works there.

Any help would be much appreciated.

If you use pandas as a library it could be a bit easier (Credits to: MrFuppes).

import pandas as pd

df = pd.read_csv(r"path/your.csv")

df['new_timestamps'] = pd.to_datetime(df['timestamps'], format='%Y-%m-%d %H:%M:%S%z')
df = df.sort_values(['new_timestamps'], ascending=True)

df.to_csv(r'path/your.csv')

If you still have errors you can also try to parse the date like this (Credits to: Zerox):

from dateutil.parser import parse
df['new_timestamps'] = df['timestamps'].map(lambda x: datetime.strptime((parse(x)).strftime('%Y-%m-%d %H:%M:%S%z'), '%Y-%m-%d %H:%M:%S%z'))

Unsure about the correct datetime-format? You can try auto-detection infer_datetime_format=True :

df['new_timestamps'] = pd.to_datetime(df['timestamps'], infer_datetime_format=True)

Tested with following sample:

df = pd.DataFrame(['2021-04-15 12:59:26+02:00','2021-04-13 12:59:26+02:00','2021-04-16 12:59:26+02:00'], columns=['timestamps'])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM