简体   繁体   中英

Sort CSV File by Timestamp 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-16T12:59:26+02:00

So year-month-day-T-hour-minute-second-timezone.

I have already tried:

data = csv.reader(open('List_32_Data.csv','r'))
data = sorted(data, key = lambda row: datetime.strptime(row[0], "%Y-%m-%d-%H-%M-%S-%HHMM"))

But i just get the error:

sre_constants.error: redefinition of group name 'H' as group 7; was group 4 at position 157

Could someone help please?

From what I can read in your error and a quick search I found your pattern "%Y-%m-%d-%H-%M-%S-%HHMM" have a problem.

From Python string to datetime - JournalDev I can read that there is Directive %z with Description

UTC offset in the form ±HHMM[SS] (empty string if the object is naive).

and example output

(empty), +0000, -0400, +1030

Therefor I'd say your pattern should be:

"%Y-%m-%d-%H-%M-%S-%z"

Regarding your error it tells a lot. You defined the %H both at your 4th and 7th group which is the case in yout pattern.

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