print dir(XXX)给出空白属性

2024-09-23 16:18:12 发布

您现在位置:Python中文网/ 问答频道 /正文

简而言之,我使用一个xlsx文件,并在使用print dir(alist)get blank属性检查一些列表时。你知道吗

neglist = neglist.tolist()

此时我想检查evth是否正常:

def check_variab (variab):
    print "The type is %s" % type(variab)
    print "Its length = %i" % len(variab)
    print "Its attributes are:" % dir(variab)

print 'neglist'
check_variab(neglist)

但我得到的是:

type: list
length: 19
attributes: 

没有属性打印,虽然类型是列表的权利,其长度和内容是确定的。你知道吗

有人能解释为什么会这样吗?你知道吗


Tags: 文件列表get属性checktypedirxlsx
1条回答
网友
1楼 · 发布于 2024-09-23 16:18:12

您忘了使用%s占位符,因此不会对任何内容进行插值。添加%s%r

print "Its attributes are %s:" % dir(variab)
#                         ^^ A placeholder for the value

如果没有占位符,您将看不到任何内容,事实上:

>>> variab = ['foo', 'bar']
>>> dir(variab)
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
>>> print "Its attributes are:" % dir(variab)
Its attributes are:
>>> print "Its attributes are %s:" % dir(variab)
Its attributes are ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']:

您想在列表上使用standard sequence operations。你知道吗

相关问题 更多 >