简体   繁体   中英

TypeError: an integer is required (got type datetime.datetime) when trying to convert a datetime object within a dictionary to a string

So I'm working in Python 3.7.4 with user-inputted dates, that are stored by another program in a variables in a dictionary format. These can potentially be any date in the immediate past. For instance November 6th 2019 looks like this:

{'Select start date': datetime.datetime(2019, 11, 6, 0, 0)}

I don't care about the item label, but I'd like to convert this dictionary date to the format 06 Nov 2019 (which I know is strftime('%d %b %Y') ) but I'm not sure how to make it understand that the above is a datetime object and do a conversion on it when it's actually a dictionary object, and without throwing up errors.

I've read a lot on here about this but almost all questions here just look at either today's date (so datetime.datetime.now() is used), or a specific date, rather than a user-inputted date that could vary, and lives inside a dictionary. I've already seen plenty of stuff like this:

import datetime
d = datetime.datetime(2019, 11, 06, 0, 0, 0, 000000)
d.strftime("%a %b %d %Y %H:%M:%S %z")

...but it doesn't seem to apply in exactly this case. Commands like strftime and strptime don't seem to work because of the dictionary format, and I can't use static examples like the above because the actual date that I want to convert won't always be the same. How can I make this work without going some crazy long way using string manipulation? I feel like there's a really easy solution to this that I'm missing.

Code example (that doesn't work):

import datetime

dic = {'Select start date': datetime.datetime(2019, 11, 7, 0, 0)}

for key, value in dic.items():
     d = datetime.datetime(value)
     d.strftime("%d %b %Y")   

Produces the error:

TypeError: an integer is required (got type datetime.datetime)

I see now what is happening. The line d = datetime.datetime(value) is the issue, you are passing a datetime object to the datetime.datetime() method which is why you get the valueError. This is unnecessary because you are already getting a datetime object out of your dictionary, so there is no reason to construct a new one. Here's the fixed code:

import datetime

dic = {'Select start date': datetime.datetime(2019, 11, 7, 0, 0)}

for key, datetime_obj in dic.items():
     datetime_str = datetime_obj.strftime("%d %b %Y")
     print(datetime_str)

An easy way to debug this is to use type() . So for example:

for key, value in dic.items():
    print(type(value))

will give you <class datetime.datetime > . Looking up the documentation for datetime.datetime() will tell you that this method only accepts integers.

Get value from dictionary and manipulate it, look like this,

dic = {'Select start date': datetime.datetime(2019, 11, 6, 0, 0)}

for v in dic.values():
     d = datetime.datetime(v)
     d.strftime("%a %b %d %Y %H:%M:%S %z")

another way is,

for key, value in dic.items():
     d = datetime.datetime(value)
     d.strftime("%a %b %d %Y %H:%M:%S %z")

Learn more about dictionary in python in this link

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