【发布时间】:2015-01-26 05:09:07
【问题描述】:
我正在尝试在以“.torrent”一词结尾的列表中查找所有数据。为此,我使用了正则表达式。当我写这篇文章时:
for k in link_torrent2:
m=re.findall(r'\S+\.torrent',link_torrent2[k])
if m:
link2.append(m)
我遇到了一个错误:
list indices must be integers, not str
所有代码:
import re
link2=[]
link_torrent1=[u'#', u'/torrent_download/3797378/THE+BLACKLIST+%282014%29+S02E02+x264+1080p%28WEB-DL%29+eng+NLsubs+TBS.torrent',
u'/category/581/', u'/torrent/3797378/THE+BLACKLIST+%282014%29+S02E02+x264+1080p%28WEB-DL%29+eng+NLsubs+TBS.html',
u'/torrent_download/3795431/The+Blacklist+S02E02+720p+HDTV+x264+AAC+-+Ozlem.torrent', u'/category/581/',
u'/torrent/3795431/The+Blacklist+S02E02+720p+HDTV+x264+AAC+-+Ozlem.html', u'/torrent_download/3795314/The.Blacklist.S02E02.HDTV.x264-ChameE.torrent']
link_torrent2=[str(x) for x in link_torrent1]
print link_torrent1
for k in link_torrent2:
m=re.findall(r'\S+\.torrent',link_torrent2[k]) ##here shows error
if m:
link2.append(m)
print m
【问题讨论】:
-
声明必须是
m=re.findall(r'\S+\.torrent',k) -
那是因为你使用了迭代器。