从列表写入csv文件,10 x 10

2024-04-24 01:08:14 发布

您现在位置:Python中文网/ 问答频道 /正文

with open('network_csv_01.csv', 'w') as f:
    writer = csv.writer(f)
    for email in my_network_emails:
        writer.writerow([email])

我想写一个csv文件,其中包含115封电子邮件列表中的10个元素,以及另一个包含10个元素的02文件,以此类推

有人能帮我吗 非常感谢


Tags: 文件csvin元素列表foremailmy
1条回答
网友
1楼 · 发布于 2024-04-24 01:08:14

第1步:将115封邮件分成12个列表,其中包含10封邮件(最后一封只包含5封邮件)

步骤2:遍历列表列表并将其写入csv

def chunks(l, n):
    """Splits a list into n number of lists inside a list."""
    n = max(1, n)
    return [l[i:i+n] for i in range(0, len(l), n)]

email_list = chunks(list_of_115_emails, 10) #returns a list of list,
i = 1
for emails in email_list:
    with open(f'network_csv_{i}.csv', 'w') as f:
        writer = csv.writer(f)
        for email in emails:
            writer.writerow([email])
    i += 1

相关问题 更多 >