Python array is read-only, cannot add values

I am new to Python. The following code throws an error when trying to add 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()

Mistake

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

+3
source share
4 answers

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

+6
source

Do you want to add to the array?

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

: e_rcs.append = float(cols.group(2)) append e-rcs . , .

+6

append - . , .

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

:

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()

Python .array, , .. .

, NumPy, n- . NumPy FORTRAN .

0

Source: https://habr.com/ru/post/1721525/


All Articles