如何让 Python csv.writer & csv.writerows 写入下一行,而不是覆盖之前的单元格?

How to get Python csv.writer & csv.writerows to write to next lines, instead of writing over previous cells?

我正在尝试让 Python 将 5 行文本写入 CSV 文件,而不是继续从单元格 [A1] 重写。

目前,程序可以写入第一个单元格,将我需要的变量填充到文本中。尽管 csv.write 和 csv.write 行不断覆盖单元格 [A1] 而不是将它们添加到底部,例如单元格 [A10]。

有没有我可以使用的功能可以跳到例如单元格 [A10] 每次写入我在下面提供的循环中时?

我曾尝试使用 newline='' 功能,但我不确定这是否相关。我对 python 很陌生。

import csv

with open("TGC_Mailout_001_13-05-2019_retarget.csv") as file:
    reader = csv.reader(file)
    next(reader) #skip header row
    for email, name, corp in reader:
        contents = [[f'Hi {name} I hope this email finds you well.'],
                    [""],
                    [f'I was wondering if you had a chance to take a look at my previous email regarding the Wellness - Mindfulness program opportunity for {corp}.'],
                    [""],
                    ['It would be great to connect with you for a quick chat to let you know more about the program and see what you are currently doing in the space of wellness.'],
                    [""],
                    [f'With stress rising around the demanding nature of work, long hours & technology reshaping working lives (always being connected), I believe {corp} would really benefit from this program.'],
                    [""],
                    ['Kind regards']]
        retarget = open("TGC_Retarget_001_21-05-2019.csv", 'w')
        #Below block is getting py to dump the text into CSV, but it is rewriting over the text each time it reads and writes 
        #We want the below to skip to the bottom of last line of text, then keep dumping the text with new variables from CSV file 
        with retarget:
            writer = csv.writer(retarget)
            writer.writerows(contents)
            print ('Writing complete')

期望的输出:

,

嗨,肖恩,希望这封邮件能找到你。

我想知道您是否有机会查看我之前关于公司健康 - 正念计划机会的电子邮件。

很高兴与您联系进行快速聊天,让您更多地了解该计划并了解您目前在 space 健康方面所做的事情。

随着工作性质、长时间工作和技术重塑工作生活(始终保持联系)带来的压力不断增加,我相信公司将从该计划中真正受益。

亲切的问候

,

嗨,嘉莉,希望这封邮件能找到你。

我想知道您是否有机会查看我之前关于公司健康 - 正念计划机会的电子邮件。

很高兴与您联系进行快速聊天,让您更多地了解该计划并了解您目前在 space 健康方面所做的事情。

随着工作性质、长时间工作和技术重塑工作生活(始终保持联系)带来的压力不断增加,我相信公司将从该计划中真正受益。

亲切的问候

,

等等等 x 100

您在 w 模式下重复打开输出文件,这会在写入前截断文件。改为以 a(追加)模式打开:

    retarget = open("TGC_Retarget_001_21-05-2019.csv", 'a')

或者更好的是,在 循环之前 打开文件并保持打开状态。

import csv

with open("TGC_Mailout_001_13-05-2019_retarget.csv") as file, \
     open("TGC_Retarget_001_21-05-2019.csv", 'w') as retarget:
    reader = csv.reader(file)
    writer = csv.writer(retarget)
    next(reader) #skip header row
    for email, name, corp in reader:
        contents = [[f'Hi {name} I hope this email finds you well.'],
                    [""],
                    [f'I was wondering if you had a chance to take a look at my previous email regarding the Wellness - Mindfulness program opportunity for {corp}.'],
                    [""],
                    ['It would be great to connect with you for a quick chat to let you know more about the program and see what you are currently doing in the space of wellness.'],
                    [""],
                    [f'With stress rising around the demanding nature of work, long hours & technology reshaping working lives (always being connected), I believe {corp} would really benefit from this program.'],
                    [""],
                    ['Kind regards']]

        writer = csv.writer(retarget)
        writer.writerows(contents)
    print ('Writing complete')