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

时间:2019-05-24 13:05:05

标签: python csv reader writer

我正在尝试让Python将5行文本写入CSV文件,但又不想一直从单元格[A1]重写。

当前,程序可以写入第一个单元格,将我需要的变量填充到文本中。虽然csv.write和csv.writerows会不断覆盖单元格[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')

所需的输出:

肖恩,我希望这封电子邮件对您有帮助。

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

与您联系以进行快速聊天,让您更多地了解该计划,并了解您目前在健康方面正在做什么。

随着工作的苛刻性,工作时间的延长和技术的改变(总是保持联系),压力越来越大,我相信公司将从该计划中真正受益。

亲切的问候

嗨,嘉莉,希望这封电子邮件对您有帮助。

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

与您联系以进行快速聊天,让您更多地了解该计划,并了解您目前在健康方面正在做什么。

随着工作的苛刻性,工作时间的延长和技术的改变(总是保持联系),压力越来越大,我相信公司将从该计划中真正受益。

亲切的问候

等等等x 100

1 个答案:

答案 0 :(得分:0)

您将反复以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')