[英]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.所以我在 Python 3.7.4 中使用用户输入的日期,这些日期由另一个程序以字典格式存储在变量中。 These can potentially be any date in the immediate past.
这些可能是最近的任何日期。 For instance November 6th 2019 looks like this:
例如,2019 年 11 月 6 日是这样的:
{'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.我不关心项目标签,但我想将这个字典日期转换为 06 Nov 2019 的格式(我知道是
strftime('%d %b %Y')
)但我不确定如何让它明白上面是一个日期时间对象,并在它实际上是一个字典对象时对其进行转换,并且不会抛出错误。
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.我在这里阅读了很多关于此的内容,但这里几乎所有问题都只关注今天的日期(因此使用了
datetime.datetime.now()
)或特定日期,而不是用户输入的可能会有所不同的日期,住在字典里。 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.由于字典格式的原因,
strftime
和strptime
之类的命令似乎不起作用,而且我不能使用像上面这样的静态示例,因为我想要转换的实际日期并不总是相同的。 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. d = datetime.datetime(value)
行是问题所在,您将 datetime 对象传递给datetime.datetime()
方法,这就是您获得 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.这是不必要的,因为您已经从字典中获取了一个 datetime 对象,因此没有理由构建一个新对象。 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()
.调试它的一种简单方法是使用
type()
。 So for example:例如:
for key, value in dic.items():
print(type(value))
will give you <class
datetime.datetime >
.会给你
<class
datetime.datetime >
。 Looking up the documentation for datetime.datetime() will tell you that this method only accepts integers.查看 datetime.datetime() 的文档会告诉您此方法只接受整数。
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 在此链接中了解有关python中字典的更多信息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.