0

time_list is a list of tuples, and within each tuple is an integer and two strings, the two strings are in a date format but I want to change them into actual datetime objects. Here's what I do:

for row in time_list:
    row[1] = datetime.strptime(row[1], "%Y-%m-%d %H:%M:%S.%5N")
    row[2] = datetime.strptime(row[2], "%Y-%m-%d %H:%M:%S.%5N")
    delta = row[2] - row[1]
    timedelta.append(delta)

I get a different error when I remove the %5N, but that's not what I'm trying to do so I won't bother with that. I do know that %5N is causing the problem. The error I get is:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\user\Programs\time_database.py", line 50, in search2
    row[1] = datetime.strptime(row[1], "%Y-%m-%d%t%H:%M:%S.%5N")
  File "C:\Python27\lib\_strptime.py", line 317, in _strptime
    (bad_directive, format))
ValueError: '5' is a bad directive in format '%Y-%m-%d %H:%M:%S.%5N'
2
  • Can you please post sample input? Commented Aug 21, 2012 at 15:46
  • 1
    This is not the problem you're having here, but you're going to have a problem because tuples are immutable- you can't change the values of row[1] and row[2] if they really are tuples. Commented Aug 21, 2012 at 15:49

1 Answer 1

3

%5N is not supported by the strftime method used in Python. Use %f (microseconds) instead:

datetime.strptime(row[1], "%Y-%m-%d %H:%M:%S.%f")
Sign up to request clarification or add additional context in comments.

Comments

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.