Python AttributeError:'list'对象属性'append'为只读//regexp组(1)/大文件xml
我需要从大型站点地图XML文件中选择数据,以获得如下内容:Python AttributeError:'list'对象属性'append'为只读//regexp组(1)/大文件xml,python,xml,Python,Xml,我需要从大型站点地图XML文件中选择数据,以获得如下内容: (title,img,link,txt) (title,img,link,txt) (title,img,link,txt) 但在测试我的代码时,我遇到了以下错误: # Traceback (most recent call last): File "./u2.py", line 91, in <module> s.pluj(x,i) File "./u2.py", line 46, in pluj tab
(title,img,link,txt)
(title,img,link,txt)
(title,img,link,txt)
但在测试我的代码时,我遇到了以下错误:
# Traceback (most recent call last): File "./u2.py", line 91, in <module>
s.pluj(x,i) File "./u2.py", line 46, in pluj
tab.append = (xx.group(1)) AttributeError: 'list' object attribute 'append' is read-only
这是我的密码:
#!/usr/bin/env python
import linecache
import re
def w2(arg1):
wiersz = linecache.getline('a.xml', arg1)
return wiersz
count = len(open('a.xml', 'rU').readlines())
#print count
class rep:
# tab = []
def pluj(self, linia, nr):
adres1 = r'\<loc\>\<\!\[CDATA\[(.*?)\]\]\>\<\/loc\>'
foto1 = r'\<image\:loc\>\<\!\[CDATA\[(.*?)\]\]\>\<\/image\:loc\>'
opis1 = r'\<image\:caption\>\<\!\[CDATA\[(.*?)\]\]\>\<\/image\:caption\>'
title1 = r'\<\image\:title\>\<\!\[CDATA\[(.*?)\]\]\>\<\/image\:title\>'
self.linia = linia
#print linia
tab = []
adres = re.compile(adres1)
foto = re.compile(foto1)
opis = re.compile(opis1)
title = re.compile(title1)
print nr
if re.match(adres, linia):
xx = adres.search(linia)
tab.append = (xx.group(1))
# return xx.group(1)
if re.match(foto, linia):
xx = foto.search(linia)
tab.append = (xx.group(1))
# return xx.group(1)
if re.match(opis, linia):
xx = opis.search(linia)
tab.append = (xx.group(1))
# return xx.group(1)
if re.match(title, linia):
xx = title.search(linia)
tab.append = (xx.group(1))
# return xx.group(1)
else: print "nope"
##################
# end rep
#################
s = rep()
i = 0
while i <= count:
x = w2(i)
#print s.pluj(x,i)
s.pluj(x,i)
i += 1
print s.tab
您正试图通过分配来更改python列表追加。您必须使用append
In [24]: l = []
In [25]: l.append = 5 # wrong
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-25-cc4bc6747222> in <module>()
----> 1 l.append = 5
AttributeError: 'list' object attribute 'append' is read-only
In [26]: l.append(5) # correct way
In [27]: l
Out[27]: [5]
tab.append=xx.group1不正确将其更改为tab.appendxx.group1thx它工作正常