python – How to add a new column to a CSV file?
python – How to add a new column to a CSV file?
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 incsv.writer
. By default it is
set torn
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 withwriterows
. 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 twowith
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:
Im surprised no one suggested Pandas. Although using a set of dependencies like Pandas might seem more heavy-handed than is necessary for such an easy task, it produces a very short script and Pandas is a great library for doing all sorts of CSV (and really all data types) data manipulation. Cant argue with 4 lines of code:
import pandas as pd
csv_input = pd.read_csv(input.csv)
csv_input[Berries] = csv_input[Name]
csv_input.to_csv(output.csv, index=False)
Check out Pandas Website for more information!
Contents of output.csv
:
Name,Code,Berries
blackberry,1,blackberry
wineberry,2,wineberry
rasberry,1,rasberry
blueberry,1,blueberry
mulberry,2,mulberry
python – How to add a new column to a CSV file?
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):
if row[0] == Name:
writer.writerow(row+[Berry])
else:
writer.writerow(row+[row[0]])
Maybe something like that is what you intended?
Also, csv stands for comma separated values. So, you kind of need commas to separate your values like this I think:
Name,Code
blackberry,1
wineberry,2
rasberry,1
blueberry,1
mulberry,2