python csv模块追加列_如何使用Python向CSV文件中添加新列?

1586010002-jmsa.png

I have several CSV files that look like this:

Input

Name Code

blackberry 1

wineberry 2

rasberry 1

blueberry 1

mulberry 2

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)

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']

>>>

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='\n')

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)

Please note

the lineterminator parameter in csv.writer. By default it is

set to '\r\n' and this is why you have double spacing.

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.

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:

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值