views:

26

answers:

2

hey guys, when i run:

with open('file00.txt') as f00:
        for line in f00:
            farr=array.append(float(line))
            print "farr= ",farr

i get:

farr=array.append(float(line))

AttributeError: 'module' object has no attribute 'append'

does anyone know why I get this? do I have to import something? am I doing it completely wrong?

thanks

A: 

I am assuming that you want to do something like this instead:

values = []
with open('file00.txt') as f00:
    for line in f00:
        value = float(line)
        values.append(value)
        print "farr= ", value

That way the values list will contain all values.

WoLpH
ahhh I see I have done it completely wrong :P Thank you
pjehyun
+1  A: 

To append to an array, you must create the array (as an instance of the array.array type with the appropriate type code), giving it a name, and call append on that name - that is, on the instance, definitely not on the module.

So, for example:

>>> import array
>>> x = array.array('d')  # array of double-precision floats
>>> x.append(1.23)
>>> x
array('d', [1.23])
>>> 

and so on. Of course, you could also use a list instead of the array.array('d') (precious if you want to append values of different types, or of non-elementary types), but the principles are identical: you make an instance of list, then call append on the instance (through the name you gave it on creation), definitely not on any module!

Alex Martelli