简体   繁体   English

如何将每一行写入 csv 文件?

[英]How can I write each row to a csv file?

I currently have code that is writing to a csv file, however, it keesp replacing the first row of the file and overwriting it instead of writing of it.我目前有正在写入 csv 文件的代码,但是,它不断替换文件的第一行并覆盖它而不是写入它。

NOTE: I have this function inside a for loop inside main().注意:我在 main() 内的 for 循环中有这个 function。

def write_csv_report(filename, region, data, current, server1, server2=False):
    if not os.path.exists(os.path.dirname(filename)):
        try:
            dir = os.makedirs(os.path.dirname(filename))
            
            header = ['region','old_dns_server', 'proposed_dns_server1', 'proposed_dns_server2']
            data = [region, current, server1, server2]

            with open(filename, 'a') as file:
                writer = csv.writer(file)
                writer.writerow(header)
                writer.writerow(data)

        except OSError as exc:
            if exc.errno != errno.EEXIST:
                raise

When I run this function in my script, it writes to the file but only one entry is added.当我在脚本中运行此 function 时,它会写入文件,但只添加一个条目。

Is there any suggestions or solution where I can add multiple entries?有什么建议或解决方案可以添加多个条目吗? I have looked at other sage questions and googled but it looks like I already have the necessary changes needed to achieve this outcome, not sure what I'm missing.我查看了其他圣人问题并进行了谷歌搜索,但看起来我已经进行了实现此结果所需的必要更改,但不确定我缺少什么。

For csv.writer (and csv.reader ), you need to open the file with newline='' .对于csv.writer (和csv.reader ),您需要使用newline=''打开文件

You also write the header every time, which is a bit odd.你还每次都写 header,这有点奇怪。

Here's a minimal example that does more-or-less what you want:这是一个或多或少做你想要的最小示例:

import csv

header = ['region','old_dns_server', 'proposed_dns_server1', 'proposed_dns_server2']

data1 = ['foo', 'bar', 'bar', 'quux']
data2 = ['foo1', 'bar1', 'bar1', 'quux2']

filename = 'test.csv'

with open(filename, 'w', newline='') as file:
    writer = csv.writer(file)
    writer.writerow(header)
    writer.writerow(data1)

with open(filename, 'a', newline='') as file:
    writer = csv.writer(file)
    writer.writerow(data2)

giving:给予:

region,old_dns_server,proposed_dns_server1,proposed_dns_server2
foo,bar,bar,quux
foo1,bar1,bar1,quux2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在csv文件的每一行中写入一个值? - How to write a value in each row of a csv file? 如何在 csv 文件的每一行中写入字典的每一项? - how to write each items of dictionary in each row of csv file? 如何将多个列表中的元素写入到csv中,其中每个元素都属于每一行? - How to can I write elements from several lists to csv in which each element belongs to each row? 我有一个csv文件,我想将csv文件的每一行提取到不同的csv文件中。 我怎样才能做到这一点? - I have a csv file and i want to extract each row of csv file into different csv file . how can i do that? 如何将字符串列表写入 CSV 文件,列表中的每一项都在一个新行中? - How do I write a list of strings to a CSV file, with each item of the list on a new row? 如何编写和读取每行的csv文件python - How to write and read csv file each row python 如何使用 python 将 csv 文件中的每 7 行转换为单行? - How can I transform each 7 row to single row in csv file with python? 为 csv 中的每一行编写一个文本文件 - Write a text file for each row in a csv 如何将字典写入csv文件? - How can I write a dictionary to a csv file? 如何使用Pandas将多进程中的每个进程写入单独的csv文件? - How can I write each process from multiprocessing to a separate csv file using pandas?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM