1

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.

1 Answer 1

1

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'])
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you very much for your answer! It worked for me now. For some reason it still gave me the same error, however when I changed it to df['new_timestamps'] = df['timestamps'].map(lambda x: datetime.strptime((parser.parse(x)).strftime('%Y-%m-%d %H:%M:%S%z'), '%Y-%m-%d %H:%M:%S%z')) it worked. Just wanted to add this bit of information in case someone runs into the same problem.
Glad I could help. I updated my answer to include your findings for future readers. Please let me know if that is fine by you. Happy Coding!
You should not need the datetime lib for parsing these strings (nice ISO8601), and neither dateutil ! pd.to_datetime will do the trick more readable and more efficiently.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.