tags:

views:

386

answers:

4

I am new to Python. The following code is causing an error when it attempts to append values to an array. What am I doing wrong?

import re
from array import array

freq_pattern = re.compile("Frequency of Incident[\(\)A-Za-z\s]*\.*\s*([\.0-9]*)")
col_pattern = re.compile("([-\.0-9]+)\s+([-\.0-9]+)\s+([-\.0-9]+)\s+([-\.0-9]+)\s+([-\.0-9]+)")
e_rcs = array('f')

f = open('example.4.out', 'r')

for line in f:
    print line,

    result = freq_pattern.search(line)
    if result:
        freq = float(result.group(1))

    cols = col_pattern.search(line)
    if cols:
        e_rcs.append = float(cols.group(2))

f.close()

Error

Traceback (most recent call last):
File "D:\workspace\CATS Parser\cats-post.py", line 31, in e_rcs.append = float(cols.group(2)) AttributeError: 'list' object attribute 'append' is read-only attributes (assign to .append)

+5  A: 

Do you want to append to the array?

e_rcs.append( float(cols.group(2)) )

Doing this: e_rcs.append = float(cols.group(2)) replaces the append method of the array e-rcs with a floating-point value. Rarely something you want to do.

S.Lott
He's not calling a list an array. He's using an array, from the array module that is part of Python's standard library.
jamessan
I have n floats to store. Traditionally I would use arrays in other languages. That is what I am trying to use in Python with little success. What data type should I be using in Python?
Jared Brown
@Jared Brown: numpy arrays are fine. Some people say "array" when they're using the built-in "list" type. I failed to read the numpy array part at the beginning.
S.Lott
You probably want a plain ol' list. It is similar to vanilla arrays in other languages.
Nick Presta
@Nick What is the advantage to using a List in this case over an array('f')?
Jared Brown
@S.Lott So is numpy part of Python? Is using array('f') using numpy?
Jared Brown
@Jared Brown. `numpy` is an add-on library. It's similar to `array` in that it's external. Both are optimized for storage of large, large numbers of similar values. A `list` is built-in, does not require an `import` and is a more general (but slower) structure.
S.Lott
in the scientific community, i've never seen anyone use the `array` library. *everyone* uses numpy. just an FYI if anyone was curious.
Autoplectic
@Autoplectic: I read "numpy" even though the code actually says "array". I'm so used to `numpy` that I assumed it without benefit of evidence. :-(
S.Lott
+3  A: 

append is a method. You're trying to overwrite it instead of calling it.

e_rcs.append(float(cols.group(2)))
jamessan
+3  A: 

You are assigning to the append() function, you want instead to call .append(float(cols.group(2))).

Brent Newey
Of course, stupid mistake, thank you.
Jared Brown
Thank you for accepting my answer. Mine was just one of three equally correct answers.
Brent Newey
A: 

Try this instead:

import re

freq_pattern = re.compile("Frequency of Incident[\(\)A-Za-z\s]*\.*\s*([\.0-9]*)")
col_pattern = re.compile("([-\.0-9]+)\s+([-\.0-9]+)\s+([-\.0-9]+)\s+([-\.0-9]+)\s+([-\.0-9]+)")
e_rcs = [] # make an empty list

f = open('example.4.out', 'r')

for line in f:
    print line,

    result = freq_pattern.search(line)
    if result:
        freq = float(result.group(1))

    cols = col_pattern.search(line)
    if cols:
        e_rcs.append( float(cols.group(2)) ) # add another float to the list

f.close()

In Python you would only use array.array when you need to control the binary layout of your storage, i.e. a plain array of bytes in RAM.

If you are going to be doing a lot of scientific data analysis, then you should have a look at the NumPy module which supports n-dimensional arrays. Think of NumPy as a replacement for FORTRAN in doing mathematics and data analysis.

Michael Dillon