【问题标题】:How to get Python csv.writer & csv.writerows to write to next lines, instead of writing over previous cells?如何让 Python csv.writer 和 csv.writerows 写入下一行,而不是覆盖以前的单元格?
【发布时间】:2019-10-11 02:17:18
【问题描述】:

我正在尝试让 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')

期望的输出:

,

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

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

很高兴与您联系以进行快速聊天,让您了解有关该计划的更多信息并了解您目前在健康领域所做的事情。

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

亲切的问候

,

嗨,Carrie,我希望这封电子邮件能找到你。

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

很高兴与您联系以进行快速聊天,让您了解有关该计划的更多信息并了解您目前在健康领域所做的事情。

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

亲切的问候

,

等等。等等等等 x 100

【问题讨论】:

    标签: python csv reader writer


    【解决方案1】:

    您在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')
    

    【讨论】:

      猜你喜欢
      • 2021-08-13
      • 2019-10-06
      • 1970-01-01
      • 1970-01-01
      • 2014-02-05
      • 2016-07-30
      • 1970-01-01
      • 2018-01-14
      • 1970-01-01
      相关资源
      最近更新 更多