我正在尝试将多行写入Excel。 "容器及#34;索引/ len为10.虽然我遍历它,但是当我运行脚本时我只得到一行。
任何人都可以帮我吗?
# define first empty row
start_row = sheet.max_row + 1
#write to excel
for container in containers:
#Company name
company_container = container.div.h2
company = company_container.text.strip()
#City
city = container.div.a["title"]
#URL
url = container.div.div.a["href"]
#Title
title_container = container.findAll("div", {"class":"info"})
title = title_container[0].img["alt"]
#Description
description = title_container[0].p.text
#Programming language
hardskill = "Java"
#Vervolgactie (Teamleader)
vervolgactie = "Nieuw"
companyinfo = [company, city, url, title, description, hardskill, vervolgactie]
for i in range(0,len(companyinfo)):
e=sheet.cell(row=start_row,column=1+i)
e.value=companyinfo[i]
wb.save("vacatures.xlsx")
答案 0 :(得分:0)
您的问题是,您正在使用e=sheet.cell(row=start_row,column=1+i)
在同一行写出所有内容。
因为所有内容都包含在循环for container in containers
中,所以我只是在子循环后递增start_row
。
for i in range(0,len(companyinfo)):
e=sheet.cell(row=start_row,column=1+i)
e.value=companyinfo[i]
start_row +=1 #not part of the above loop but part of the overarching loop
我考虑的另一条路线是将start_row
重命名为just(这在语义上更清晰)并在循环中定义row
#start_row = sheet.max_row + 1
#write to excel
for container in containers:
#row for this data to go on.
row = sheet.max_row + 1
#Company name
company_container = container.div.h2
company = company_container.text.strip()