Python的数组是只读的,不能追加值 [英] Python Array is read-only, can't append values
问题描述
我是新来的Python。当它试图追加值到一个数组下code导致一个错误。我在做什么错了?
进口重
从阵列导入阵列freq_pattern = re.compile(事变[\\(\\)一个-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 =阵列('F')F =开放('example.4.out','R')在F线:
打印线, 结果= freq_pattern.search(线)
如果结果是:
频率=浮动(result.group(1)) COLS = col_pattern.search(线)
如果COLS:
e_rcs.append =浮动(cols.group(2))f.close()
错误
回溯(最近通话最后一个):
文件D:\\工作区\\ CATS
解析器\\ cats-post.py,第31行,在
e_rcs.append =浮动(cols.group(2))AttributeError的:
名单'对象属性'追加'是
只读属性(分配给.append)
块引用>解决方案您指定给追加()函数,你想,而不是调用.append(浮点(cols.group(2)))。
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)
解决方案You are assigning to the append() function, you want instead to call .append(float(cols.group(2))).
这篇关于Python的数组是只读的,不能追加值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!