所以有很多帮助,并且稍后会对文档进行讨论:
for row in ws.iter_rows():
for cell in row:
if cell.value and isinstance(cell.value, str) and emailRegex.match(cell.value):
mail = emailRegex.match(cell.value)
if mail:
mail = mail.group(0)
customeremails.append(mail)
print(customeremails)
打印命令让我:
[' store@xxxx.com' ;,' xxxx@yahoo.co.in' ;,' xxxx@gmail.com']
现在我想添加以下内容,tryint将此列表从单元格B2开始到新的excel文件中:
#Adding the below before print(customeremails)
os.chdir("C:\Python34")
wb = Workbook()
dest_filename = 'trial.xlsx'
ws1 = wb.active
ws1.title = sheet
i = 2
for i,mail in enumerate(customeremails):
ws1.cell(row = i, column = 2).value = mail
i = i+1
return i
wb.save(filename = dest_filename)
创建文件,重命名工作表。但我只获得列表中的一个电子邮件地址。
答案 0 :(得分:1)
这里有2个问题:
i
:无用,并阻止正确创建文件enumerate
并更改/初始化i
:选择。我让enumerate
做好工作。我的提案(向enumerate
添加了可选参数,因此i
从2
开始,行不能为0
):
for i,mail in enumerate(customeremails,2):
ws1.cell(row = i, column = 2).value = mail
wb.save(filename = dest_filename)