简体   繁体   English

datetime.datetime对象的len()

[英]len() of datetime.datetime object

I am trying to get the length of various types of data in python: 我正在尝试获取python中各种数据的长度:

 if type(rowoutClipFC[fieldnum]) in (int, float, datetime.datetime):
       lenrowoutClipFC = len(str(rowoutClipFC[fieldnum]))
 else:
       lenrowoutClipFC = len(rowoutClipFC[fieldnum])

The types I have come across so far are int(), float(), str(), str() with ascii characters, datetime.datetime. 到目前为止,我遇到的类型是int(),float(),str(),带有ascii字符,datetime.datetime的str()。

I need to convert the int(), float(), datetime.datetime objects to str() so I can get length, whereas I do not need to do this with the str() and str() with ascii. 我需要将int(),float(),datetime.datetime对象转换为str(),以便获得长度,而无需使用带有ascii的str()和str()来做到这一点。 For some reason, datetime.datetime does not pass the smell test of the first if statement, so it errors. 由于某种原因,datetime.datetime无法通过第一个if语句的气味测试,因此会出错。 I'm not sure why it isn't.... 我不确定为什么不是...

the error returns: 错误返回:

lenrowoutClipFC = len(rowoutClipFC[fieldnum])
TypeError: object of type 'datetime.datetime' has no len()

So, it's moving onto the else statement which is not what I was expecting... 因此,它正在转移到else语句中,这不是我所期望的...

EDIT: 编辑:

What I am expecting is this: 我期望的是:

for row in arcpy.SearchCursor("Strategic_Land_Resource_Planning_Area"):
    type(row.APPROVAL_DATE)

This returns datetime.datetime 这将返回datetime.datetime

The field in the table I am pulling the data from is a 'date' field. 我要从中提取数据的表中的字段是“日期”字段。 So, I guess it returns a datetime.datetime type...even though someone mentioned that there is no such thing as a datetime.datetime type? 因此,我猜它返回的是datetime.datetime类型...即使有人提到没有datetime.datetime类型这样的东西?

So, I further test: 因此,我进一步测试:

import datetime
for row in arcpy.SearchCursor("Strategic_Land_Resource_Planning_Area"):
    if type(row.APPROVAL_DATE) == datetime.datetime:
         print 'Yes"

This returns Yes 这返回Yes

So, I am not sure why my script at the top doesn't satisfy the if condition.... 因此,我不确定为什么顶部的脚本不满足if条件。

I tries using isinstance with the same reults. 我尝试使用具有相同结果的isinstance It doesn't satisfy in the if statement. 它在if语句中不满足。

It's because the type of datetime.datetime isn't 'datetime', it's 'type'. 这是因为datetime.datetime的类型不是'datetime',而是'type'。 That's because datetime.datetime is the class, not an instance of the class. 这是因为datetime.datetime是类,而不是该类的实例。

Eg 例如

>>> import datetime

>>> type(datetime.datetime)
<type 'type'>

>>> type(datetime.datetime.now())
<type 'datetime.datetime'>

>>> type(int)
<type 'type'>
>>>

The docs recommend if you're testing the type of something, to use isinstance() rather than type() (ref https://docs.python.org/2/library/functions.html#type ) 如果您要测试事物的类型,则文档建议使用isinstance()而不是type()(请参阅https://docs.python.org/2/library/functions.html#type

Try this instead: 尝试以下方法:

field = rowoutClipFC[fieldnum]
if type(field) in (int, float) or isinstance(field, datetime.datetime):
    lenrowoutClipFC = len(str(field))
else:
    lenrowoutClipFC = len(field)

Even simpler, you could just do this: 更简单的是,您可以执行以下操作:

field = rowoutClipFC[fieldnum]
lenrowoutClipFC = len(str(field))

To explain this a little 稍微解释一下

I need to convert the int(), float(), datetime.datetime objects to str() so I can get length, whereas I do not need to do this with the str() and str() with ascii. 我需要将int(),float(),datetime.datetime对象转换为str(),以便获得长度,而无需使用带有ascii的str()和str()来做到这一点。

In Python there are a number of built in sequence types, including strings, unicode strings, lists, tuples etc. You can pass any of these types to len() and a integer will be returned, as there is a protocol in C Python at least which expects these objects to support this behaviour. 在Python中,有许多内置的序列类型,包括字符串,unicode字符串,列表,元组等。您可以将这些类型中的任何一种传递给len()并且将返回整数,因为C Python中有一个协议 ,网址为至少期望这些对象支持这种行为。

>>> len("hello")
5
>>> len(["foo", "bar"]
2

Many other objects can also be passed to len() , including built-ins types like dictionaries and custom objects which implement a __len__() magic method. 许多其他对象也可以传递给len() ,包括内置类型(如dictionaries和实现__len__()魔术方法的自定义对象。

>>> len({"foo":"bar"}
1

Some objects which are not sequences / containers do not support this however, such as integer and float types like you mentioned. 但是,某些不是序列/容器的对象不支持此功能,例如您提到的integerfloat类型。 If you try and pass such objects to len() , a TypeError exception is raised. 如果尝试将此类对象传递给len() ,则会引发TypeError异常。

>>> len(5)
TypeError: object of type 'int' has no len()

Same goes for datetime objects as you've found out 发现的datetime对象也是如此

>>> from datetime import datetime
>>> len(datetime.today())
TypeError: object of type 'datetime.datetime' has no len()

And this makes sense - len() should indicate the number of items in an object. 这很有意义len()应该指示一个对象中的项目数。 What would you expect a datetime object to return? 您希望datetime对象返回什么?

So you can convert the datetime object into a string and count it's characters, but that doesn't really tell you much. 因此,您可以将datetime对象转换为字符串并计算其字符数,但这并不能告诉您太多信息。

>>> datetime.datetime.today()
'2014-04-12 00:36:03.829979'
>>> len(str(datetime.today()))
26

Ultimately you shouldn't be trying to call len() on a datetime object at all - so you should rethink what you are trying to achieve with this code. 最终,您根本不应该尝试在datetime对象上调用len() ,因此您应该重新考虑您要使用此代码实现的目标。

Can you try this: 你可以尝试一下:

import datetime
for row in arcpy.SearchCursor("Strategic_Land_Resource_Planning_Area"):
    if isinstance(row.APPROVAL_DATE), datetime.datetime):
         print "Yes"
    else:
         print "No"

Does that print "Yes"? 是否打印“是”?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM