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
6
  • 1
    is using pandas an option? Commented Feb 18, 2021 at 9:19
  • Why are you using strptime() instead of just creating a datetime object directly from the values in the file? Commented Feb 18, 2021 at 9:19
  • @EzerK no, preferablly not. I want to make it work with my current code. Commented Feb 18, 2021 at 9:21
  • @Barmar I have not really though about it... Commented Feb 18, 2021 at 9:21
  • +1 for using pandas - if you want to dig deep, skip using csv as well and write your own parser. But why not enjoy some convenience? e.g. have a look at convert columns into one datetime column in pandas. Commented Feb 18, 2021 at 9:24

1 Answer 1

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

5 Comments

thanks. This looks good. Can you just explain what the * and the map function just did?
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.
So can I assume that this will always work 100%?
I'm not going to bet my life on it, but I can't see why it wouldn't work.
It should work as long as the columns in your CSV are in the same order as the arguments to datetime.datetime()

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.