How to add padding zeros to a datetime object in Python?

Viewed 411

I have a file which looks like this:

London  XXX Europe  2020    9   7   0   0   0   2   2020    9   7   0   11  35  2   57
Tanger  XXX Africa  2020    9   7   0   29  54  2   2020    9   7   23  57  16  2   29
Doha    XXX Asia    2020    9   7   0   57  23  2   2020    9   7   23  58  48  2   11

I'am trying to combine index 3,4,5,6,7,8 into a datetimeobject with Year, Month, Day, Hour, Minute, Second. I try to do the same with end_time. However, the zeros in my file seem to produce some weird output.

This is my code:

path = r'c:\data\EK\Desktop\Python Microsoft Visual Studio\Extra\test_datetime.txt'

with open(path, 'r') as input_file:
    reader = csv.reader(input_file, delimiter='\t')
    for row in reader:
        start_time = (row[3] + row[4] + row[5] + row[6] + row[7] + row[8])  
        end_time = (row[10] + row[11] + row[12] + row[13] + row[14] + row[15])

        start_time = datetime.datetime.strptime(start_time, "%Y%m%d%H%M%S")
        end_time = datetime.datetime.strptime(end_time, "%Y%m%d%H%M%S")

        print(start_time)
        print(end_time)

This is my current output:

2020-09-07 00:00:00
2020-09-07 01:13:05
2020-09-07 02:09:54
2020-09-07 23:57:16
2020-09-07 05:07:23
2020-09-07 23:58:48

This is my expected output:

2020-09-07 00:00:00
2020-09-07 00:11:35
2020-09-07 00:29:54
2020-09-07 23:57:16
2020-09-07 00:57:23
2020-09-07 23:58:48
1 Answers

The problem is that when you concatenate the fields like row[3] + row[4] + row[5] + row[6] + row[7] + row[8] all the single-digit fields have no leading zeroes, so they aren't parsed properly with strptime().

You could use a string formatting function to add leading zeroes, but there's no reason to use strptime() in the first place. Just call datetime.datetime() to create an object directly from the values.

start_time = datetime.datetime(*map(int, row[3:9]))
end_time = datetime.datetime(*map(int, row[10:16]))
Related