如何将新列添加到 CSV 文件行的开头?

发布于 2024-10-15 09:45:37 字数 331 浏览 2 评论 0原文

我有一个 csv 文件,其中有 6 到 8 列。
例如:

ID Test Description file-name module view path1 path2 

我想在开头添加新列 (Node)。
前任:

Node ID Test Description file-name module view path1 path2 

I have one csv file in which I have 6 to 8 column.
Ex:

ID Test Description file-name module view path1 path2 

I want to add new column (Node) to the beginning.
Ex:

Node ID Test Description file-name module view path1 path2 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

土豪我们做朋友吧 2024-10-22 09:45:37

使用 csv 模块的 DictReaderDictWriter 类可以很容易地做到这一点。下面是一个示例,它一次性读取旧文件并写入新文件。

DictReader 实例将文件的每个逻辑行或行作为字典返回,其键是字段名称。您可以显式指定字段名称,也可以从文件的第一行读取它们(如下面的示例所示)。

在创建 DictWriter 实例时,您必须指定所需的字段名称,并且字段名称的顺序定义了它们在输出文件的每一行上出现的顺序。在这种情况下,新的字段名称只是添加到输入文件中的名称列表的开头 - 无论它们是什么。

import csv

with open('testdata.txt', 'r', newline='') as inf, \
     open('testdata2.txt', 'w', newline='') as outf:
    csvreader = csv.DictReader(inf)
    fieldnames = ['Node'] + csvreader.fieldnames  # Add column name to beginning.
    csvwriter = csv.DictWriter(outf, fieldnames)
    csvwriter.writeheader()
    for node, row in enumerate(csvreader, start=1):
        csvwriter.writerow(dict(row, Node='node %s' % node))

如果这是输入文件的内容:

ID,Test Description,file-name,module,view,path1,path2
id 1,test 1 desc,test1file.txt,test1module,N,test1path1,test1path2
id 2,test 2 desc,test2file.txt,test2module,Y,test2path1,test2path2
id 3,test 3 desc,test3file.txt,test3module,Y,test3path1,test3path2
id 4,test 4 desc,test4file.txt,test4module,N,test4path1,test4path2
id 5,test 5 desc,test5file.txt,test5module,Y,test5path1,test5path2

这将是运行脚本后生成的输出文件的内容:

Node,ID,Test Description,file-name,module,view,path1,path2
node 1,id 1,test 1 desc,test1file.txt,test1module,N,test1path1,test1path2
node 2,id 2,test 2 desc,test2file.txt,test2module,Y,test2path1,test2path2
node 3,id 3,test 3 desc,test3file.txt,test3module,Y,test3path1,test3path2
node 4,id 4,test 4 desc,test4file.txt,test4module,N,test4path1,test4path2
node 5,id 5,test 5 desc,test5file.txt,test5module,Y,test5path1,test5path2

请注意,使用 dict(row, Node='node %s 将字段的数据添加到每一行' % node) 仅当字段名称是有效关键字参数(即有效的 Python 标识符)时才有效,如 Node

有效标识符只能由字母、数字和下划线组成,但不能以数字或下划线开头,并且不能是语言关键字,例如 classfor返回全局传递等。

此限制的解决方法是手动更新每个 row 字典,因为字段名称不能用作关键字参数:

    fieldnames = ['Invalid-Identifier''] + csvreader.fieldnames  # Add column name.
    ...
    for node, row in enumerate(csvreader, 1):
        row['Invalid-Identifier'] = 'node %s' % node  # add new field and value
        csvwriter.writerow(row)

It would be fairly easy to do using the csv module's DictReader and DictWriter classes. Here's an example that reads the old file and writes the new one in single pass.

A DictReader instance returns each logical line or row of the file as a dictionary whose keys are the field names. You can explicitly specify the field names or they can be read from the first line of the file (as is done in the example below).

You must specify the desired field names when creating a DictWriter instance and the order of the field names defines the order they will appear on each line of the output file. In this case the new field name is simply added to beginning of the list of names from the input file — whatever they may be.

import csv

with open('testdata.txt', 'r', newline='') as inf, \
     open('testdata2.txt', 'w', newline='') as outf:
    csvreader = csv.DictReader(inf)
    fieldnames = ['Node'] + csvreader.fieldnames  # Add column name to beginning.
    csvwriter = csv.DictWriter(outf, fieldnames)
    csvwriter.writeheader()
    for node, row in enumerate(csvreader, start=1):
        csvwriter.writerow(dict(row, Node='node %s' % node))

If this was the contents of the input file:

ID,Test Description,file-name,module,view,path1,path2
id 1,test 1 desc,test1file.txt,test1module,N,test1path1,test1path2
id 2,test 2 desc,test2file.txt,test2module,Y,test2path1,test2path2
id 3,test 3 desc,test3file.txt,test3module,Y,test3path1,test3path2
id 4,test 4 desc,test4file.txt,test4module,N,test4path1,test4path2
id 5,test 5 desc,test5file.txt,test5module,Y,test5path1,test5path2

This would be the contents of the resulting output file after running the script:

Node,ID,Test Description,file-name,module,view,path1,path2
node 1,id 1,test 1 desc,test1file.txt,test1module,N,test1path1,test1path2
node 2,id 2,test 2 desc,test2file.txt,test2module,Y,test2path1,test2path2
node 3,id 3,test 3 desc,test3file.txt,test3module,Y,test3path1,test3path2
node 4,id 4,test 4 desc,test4file.txt,test4module,N,test4path1,test4path2
node 5,id 5,test 5 desc,test5file.txt,test5module,Y,test5path1,test5path2

Note that adding the data for a field to each row with dict(row, Node='node %s' % node) as shown only works when the field name is a valid keyword argument (i.e. valid Python identifier) — like Node.

Valid identifiers consist only of letters, digits, and underscores but not start with a digit or underscore, and cannot be language keyword such as class, for, return, global, pass, etc.

The workaround for this limitation is to update each row dictionary manually since the field name cannot be used as a keyword argument:

    fieldnames = ['Invalid-Identifier''] + csvreader.fieldnames  # Add column name.
    ...
    for node, row in enumerate(csvreader, 1):
        row['Invalid-Identifier'] = 'node %s' % node  # add new field and value
        csvwriter.writerow(row)
你曾走过我的故事 2024-10-22 09:45:37

您可以使用 CSV 模块读取 CSV 文件并写出带有附加列的编辑版本。请记住,添加列就是在每行末尾添加一个额外的条目。

使用 CSV 模块输出的示例 (http://docs.python.org/library/csv. html)

>>> import csv
>>> spamWriter = csv.writer(open('eggs.csv', 'wb'), delimiter=' ',
...                         quotechar='|', quoting=csv.QUOTE_MINIMAL)
>>> spamWriter.writerow(['Spam'] * 5 + ['Baked Beans'])
>>> spamWriter.writerow(['Spam', 'Lovely Spam', 'Wonderful Spam'])

You can use the CSV module to read in your CSV file and write out an edited version with an appended column. Remember that adding a column is adding an extra entry to the end of each line.

An example of outputting with the CSV module (http://docs.python.org/library/csv.html)

>>> import csv
>>> spamWriter = csv.writer(open('eggs.csv', 'wb'), delimiter=' ',
...                         quotechar='|', quoting=csv.QUOTE_MINIMAL)
>>> spamWriter.writerow(['Spam'] * 5 + ['Baked Beans'])
>>> spamWriter.writerow(['Spam', 'Lovely Spam', 'Wonderful Spam'])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文