Python 3:我的循环正在替换我的最后一个列表条目而不是追加到它
问题描述:
我有两个问题,但我认为解决一个问题将解决另一个问题。 我的目标是移动到几个不同的网页,并找到包含节点名称的行,我已经设法为此创建for循环工作正常。每次我的for循环再次运行它时,唯一的问题是从列表中删除最后一个节点名称条目并在其位置添加新节点名称,因此只在列表中留下一个节点名称。Python 3:我的循环正在替换我的最后一个列表条目而不是追加到它
完整的代码 - 关于以发行
webstringy = "mycompanysite.com/?NodeID="
webpage = "mycompanysite.com/?NetworkID=36"
r2 = s2.get(webpage)
bsobjswap = BeautifulSoup(r2.content)
gotopagenums = [re.findall("\d+", i.get('onclick')) for i in bsobjswap.findAll('tr', attrs={'onclick':True})]
#link = (len(gotopagenums))
print (gotopagenums)
results = open("niki2.csv", 'w', newline='')
wr2 = csv.writer(results, dialect='excel')
for i in gotopagenums:
wr2.writerows([i])
for nodeno in gotopagenums:
nodenojoin = "".join(nodeno)
weblink = [webstringy+nodenojoin]
for weblnky in weblink:
r2 = s2.get(weblnky)
bsobjswap2 = BeautifulSoup(r2.content)
nodename = [(bsobjswap2.h1.span)]
test = [nodename]
test3 = '\n'.join(str(e) for e in test)
#if test3.startswith("[<span"):
# if test3.endswith("</span>]"):
test4 = (test3[72:])
test5 = (test4[:-9])
test5 = [test5]
print (test5)
resultfile = open("niki.csv", 'w')
wr = csv.writer(resultfile, delimiter=',', dialect='excel')
for i in test5:
wr.writerows([i])
wr.writerows('\n')
现在,当我运行这个第一个CSV文件(niki2.csv)工作正常,我假定这是因为所有条目都在一个列表(每个列表条目添加到CSV中,因为我想单独行)
问题代码
for weblnky in weblink:
r2 = s2.get(weblnky)
bsobjswap2 = BeautifulSoup(r2.content)
nodename = [(bsobjswap2.h1.span)]
test = [nodename]
test3 = '\n'.join(str(e) for e in test)
#if test3.startswith("[<span"):
# if test3.endswith("</span>]"):
test4 = (test3[72:])
test5 = (test4[:-9])
test5 = [test5]
print (test5)
resultfile = open("niki.csv", 'w')
wr = csv.writer(resultfile, delimiter=',', dialect='excel')
for i in test5:
wr.writerows([i])
wr.writerows('\n')
这是我的代码的问题是一部分,我相信。当我在打印TEST5列表循环,我得到
FOR环路输出
['GG Alperton']
['GG Angel']
['GG Ashford']
['GG Barking']
['GG Bedford']
['GG Birmingham']
['GG Bolton']
['GG Bothwell Street']
['GG Bournemouth']
['GG Bracknell']
['GG Brighton London road']
['GG Brighton Madeira']
['GG Bristol']
['GG Cardiff']
['GG Chadwell Heath']
['GG Charing Cross']
['GG Chelmsford']
['GG Colchester']
['GG Crawley']
['GG Croydon']
['GG Dartford']
['GG Derby']
['GG Ealing']
['GG East Croydon']
['GG Eastbourne']
,当我在循环外面打印TEST5我得到
['GG Eastbourne']
这是最后一项,这样当我尝试写出一个csv时,它只包含这个条目。
我需要请请知道如何获得上述所有条目到一个列表中,以便我可以正确地打印到.csv。
我试着追加,映射,连接,越来越多的循环我无法弄清楚。
输出高拉夫居所
[['GG Alperton']]
[['GG Angel']]
[['GG Ashford']]
[['GG Barking']]
[['GG Bedford']]
[['GG Birmingham']]
[['GG Bolton']]
[['GG Bothwell Street']]
[['GG Bournemouth']]
[['GG Bracknell']]
[['GG Brighton London road']]
[['GG Brighton Madeira']]
[['GG Bristol']]
[['GG Cardiff']]
[['GG Chadwell Heath']]
[['GG Charing Cross']]
[['GG Chelmsford']]
[['GG Colchester']]
[['GG Crawley']]
[['GG Croydon']]
[['GG Dartford']]
[['GG Derby']]
[['GG Ealing']]
[['GG East Croydon']]
[['GG Eastbourne']]
答
更改代码:
mylist = []
for nodeno in gotopagenums:
nodenojoin = "".join(nodeno)
weblink = webstringy+nodenojoin
r2 = s2.get(weblink)
bsobjswap2 = BeautifulSoup(r2.content)
nodename = [(bsobjswap2.h1.span)]
test = [nodename]
test3 = '\n'.join(str(e) for e in test)
#if test3.startswith("[<span"):
# if test3.endswith("</span>]"):
test4 = (test3[72:])
test5 = (test4[:-9])
test5 = [test5]
mylist.append(test5)
print mylist
resultfile = open("niki.csv", 'w')
wr = csv.writer(resultfile, delimiter=',', dialect='excel')
for i in mylist:
wr.writerow(i)
你为什么要建立在你的代码不必要的名单,你不需要内部循环。
感谢您的回应,但我仍然得到相同的输出,只有列表中的最后一个条目 – vempi12
ive添加了如何输出现在看起来到我的问题的底部 – vempi12
我编辑了答案,请检查您的结果一次再次 –