从 python 列表中将递归数据写入 excel
Write recursive data in excel from python list
我有一个网页列表:html
在每个 html(i)
元素中,我提取了电子邮件地址。我将这些电子邮件地址放在列表中:email
我想生成这样的 excel 文件:
为了在 excel 文件中写下我找到的所有电子邮件地址。
由于每个 html(i)
页面可能包含不同数量的电子邮件地址,我想编写一个代码来自动考虑每页发现的不同数量的电子邮件。
我的想法与此类似:
#set the standard url to generate the full list of urls to be analyzed
url = ["url1","url2", "url3", "url-n"]
#get all the url pages' html codes
for i in range (0,len(url):
html=[urllib.urlopen(url[i]).read() for i in range(0,len(url)) ]
#find all the emails in each html page.
for i in range (0,len(url):
emails = re.findall(r'[\w\.-]+@[\w\.-]+', html[i])
#create an excel file
wb = Workbook()
#Set the excel file.
for i in range (0,len(html)):
for j in range (0, len(emails)):
sheet1.write(i, j, emails[j])
wb.save('emails contact2.xls')
当然不行。它只写入列表 html 最后一个元素中包含的电子邮件地址。有什么建议吗?
我不知道 xlwt,但考虑到你有一个 emails
的列表,每个 html
这样的东西行得通吗?
import xlwt
wb = Workbook()
for html_index, html in enumerate(html):
sheet1.write(html_index, 0, html.address)
for email_index, email in enumerate(emails_for_html):
sheet1.write(html_index, email_index + 1, email)
wb.save('email contacts.xls')
请注意,我不知道 xlwt 的具体命令,只是想模仿你的。
import xlwt
wb = Workbook()
sheet1 = wb.add_sheet("Sheet 1")
htmls = generate_htmls() #Imaginary function to pretend it's initialized.
for i in xrange(len(htmls)):
sheet1.write(i, 0, htmls[i])
emails = extract_emails(htmls[i]) #Imaginary function to pretend it's extracted
for j in xrange(len(emails)):
sheet1.write(i, j + 1, emails[i])
假设您分别为每个 html 提取列表 emails
,此代码将 html 放在第 1(索引 0)列,然后将所有电子邮件放在 index + 1
(不覆盖第一列)。
我有一个网页列表:html
在每个 html(i)
元素中,我提取了电子邮件地址。我将这些电子邮件地址放在列表中:email
我想生成这样的 excel 文件:
为了在 excel 文件中写下我找到的所有电子邮件地址。
由于每个 html(i)
页面可能包含不同数量的电子邮件地址,我想编写一个代码来自动考虑每页发现的不同数量的电子邮件。
我的想法与此类似:
#set the standard url to generate the full list of urls to be analyzed
url = ["url1","url2", "url3", "url-n"]
#get all the url pages' html codes
for i in range (0,len(url):
html=[urllib.urlopen(url[i]).read() for i in range(0,len(url)) ]
#find all the emails in each html page.
for i in range (0,len(url):
emails = re.findall(r'[\w\.-]+@[\w\.-]+', html[i])
#create an excel file
wb = Workbook()
#Set the excel file.
for i in range (0,len(html)):
for j in range (0, len(emails)):
sheet1.write(i, j, emails[j])
wb.save('emails contact2.xls')
当然不行。它只写入列表 html 最后一个元素中包含的电子邮件地址。有什么建议吗?
我不知道 xlwt,但考虑到你有一个 emails
的列表,每个 html
这样的东西行得通吗?
import xlwt
wb = Workbook()
for html_index, html in enumerate(html):
sheet1.write(html_index, 0, html.address)
for email_index, email in enumerate(emails_for_html):
sheet1.write(html_index, email_index + 1, email)
wb.save('email contacts.xls')
请注意,我不知道 xlwt 的具体命令,只是想模仿你的。
import xlwt
wb = Workbook()
sheet1 = wb.add_sheet("Sheet 1")
htmls = generate_htmls() #Imaginary function to pretend it's initialized.
for i in xrange(len(htmls)):
sheet1.write(i, 0, htmls[i])
emails = extract_emails(htmls[i]) #Imaginary function to pretend it's extracted
for j in xrange(len(emails)):
sheet1.write(i, j + 1, emails[i])
假设您分别为每个 html 提取列表 emails
,此代码将 html 放在第 1(索引 0)列,然后将所有电子邮件放在 index + 1
(不覆盖第一列)。