python列表添加元素为什么是只读的_AttributeError:“list”object attribute“append”是只读的//regexp group(1)/big file xm...

我需要从一个大的sitemap XML文件中选择数据来获得如下信息:(title,img,link,txt)

(title,img,link,txt)

(title,img,link,txt)

但是在测试我的代码时,我得到了一个错误:# Traceback (most recent call last): File "./u2.py", line 91, in

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'\\<\!\[CDATA\[(.*?)\]\]\>\<\/loc\>'

foto1 = r'\\<\!\[CDATA\[(.*?)\]\]\>\<\/image\:loc\>'

opis1 = r'\\<\!\[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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值