简而言之,我使用xlsx文件,并在使用print dir(alist)
检查某些列表时获取空白属性。
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:
虽然类型是列表,但它的长度和内容都没问题,所以没有打印任何属性。
有人能解释为什么会这样吗?
答案 0 :(得分:0)
您忘记使用%s
占位符,因此将插入 nothing 。添加%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。