Talka Talka - 1 year ago 68
Python Question

print dir(XXX) gives out blank attributes

In short, I work with an xlsx file and while checking some list with

print dir(alist)
get blank attributes.

In detail:
At first I use openpyxl to open xlsx:

def get_list_xlsx (filename, listname):
wb = openpyxl.load_workbook(filename)
sheet =
listname = np.array([[cell.value for cell in col] for col in sheet['A1:A19']])
return listname

It returns the contents of the file in the array format. I use this function to open 3 files:

neglist = []
neglist = get_list_xlsx('c:\\doc2v\\trainneg.xlsx', neglist)

I change the array format into the list format (perhaps you know how to get a xlsx file into a list at once but I found only this long way).

neglist = neglist.tolist()

At this point I want to check whether evth is Ok:

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'

But what I get is:

type: list
length: 19

![no attributes are printed though the type is list all right, its length and content is ok][1].

Could anyone explain why this happens?

Answer Source

You forgot to use a %s placeholder, so nothing will be interpolated. Add %s or %r:

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

Without that placeholder, you won't see anything, indeed:

>>> 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']:

You want to use standard sequence operations on lists.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download