如何向 CSV 文件添加新列?

How to add a new column to a CSV file?(如何向 CSV 文件添加新列?)

本文介绍了如何向 CSV 文件添加新列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个 CSV 文件,如下所示:

I have several CSV files that look like this:

Input
Name        Code
blackberry  1
wineberry   2
rasberry    1
blueberry   1
mulberry    2

我想为所有 CSV 文件添加一个新列,使其如下所示:

I would like to add a new column to all CSV files so that it would look like this:

Output
Name        Code    Berry
blackberry  1   blackberry
wineberry   2   wineberry
rasberry    1   rasberry
blueberry   1   blueberry
mulberry    2   mulberry

我目前的脚本是这样的:

The script I have so far is this:

import csv
with open(input.csv,'r') as csvinput:
    with open(output.csv, 'w') as csvoutput:
        writer = csv.writer(csvoutput)
        for row in csv.reader(csvinput):
            writer.writerow(row+['Berry'])

(Python 3.2)

(Python 3.2)

但在输出中,脚本跳过每一行,新列中只有 Berry:

But in the output, the script skips every line and the new column has only Berry in it:

Output
Name        Code    Berry
blackberry  1   Berry

wineberry   2   Berry

rasberry    1   Berry

blueberry   1   Berry

mulberry    2   Berry

推荐答案

这应该让你知道该怎么做:

This should give you an idea of what to do:

>>> v = open('C:/test/test.csv')
>>> r = csv.reader(v)
>>> row0 = r.next()
>>> row0.append('berry')
>>> print row0
['Name', 'Code', 'berry']
>>> for item in r:
...     item.append(item[0])
...     print item
...     
['blackberry', '1', 'blackberry']
['wineberry', '2', 'wineberry']
['rasberry', '1', 'rasberry']
['blueberry', '1', 'blueberry']
['mulberry', '2', 'mulberry']
>>> 

编辑,注意在py3k中你必须使用next(r)

Edit, note in py3k you must use next(r)

感谢您接受答案.在这里你有一个奖励(你的工作脚本):

Thanks for accepting the answer. Here you have a bonus (your working script):

import csv

with open('C:/test/test.csv','r') as csvinput:
    with open('C:/test/output.csv', 'w') as csvoutput:
        writer = csv.writer(csvoutput, lineterminator='
')
        reader = csv.reader(csvinput)

        all = []
        row = next(reader)
        row.append('Berry')
        all.append(row)

        for row in reader:
            row.append(row[0])
            all.append(row)

        writer.writerows(all)

请注意

  1. csv.writer 中的lineterminator 参数.默认情况下是设置为 ' ' 这就是你有双倍间距的原因.
  2. 使用列表附加所有行并将它们写入writerows 的一张照片.如果你的文件非常非常大,这个可能不是一个好主意(RAM),但对于普通文件,我认为它是更快,因为 I/O 更少.
  3. 正如这篇文章的评论中所指出的,请注意,而不是嵌套两个 with 语句,您可以在同一行中进行:

  1. the lineterminator parameter in csv.writer. By default it is set to ' ' and this is why you have double spacing.
  2. the use of a list to append all the lines and to write them in one shot with writerows. If your file is very, very big this probably is not a good idea (RAM) but for normal files I think it is faster because there is less I/O.
  3. As indicated in the comments to this post, note that instead of nesting the two with statements, you can do it in the same line:

用 open('C:/test/test.csv','r') 作为 csvinput,open('C:/test/output.csv', 'w') 作为 csvoutput:

with open('C:/test/test.csv','r') as csvinput, open('C:/test/output.csv', 'w') as csvoutput:

这篇关于如何向 CSV 文件添加新列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何向 CSV 文件添加新列?

基础教程推荐