2

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
TangerCity
  • 775
  • 2
  • 7
  • 13

1 Answers1

4

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]))
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • thanks. This looks good. Can you just explain what the `*` and the `map` function just did? – TangerCity Feb 18 '21 at 09:27
  • `map()` calls a function on all the elements of a list and returns all the results. So this converts all the strings in that list to integers. And `*` spreads a list into separate arguments to the function. – Barmar Feb 18 '21 at 09:28
  • So can I assume that this will always work 100%? – TangerCity Feb 18 '21 at 09:30
  • 1
    I'm not going to bet my life on it, but I can't see why it wouldn't work. – Barmar Feb 18 '21 at 09:31
  • It should work as long as the columns in your CSV are in the same order as the arguments to `datetime.datetime()` – Barmar Feb 18 '21 at 09:31