Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a new column to a CSV file?

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 
like image 902
fairyberry Avatar asked Jun 17 '12 10:06

fairyberry


People also ask

How to append new column in exisiting CSV file?

add new column in exisiting csv file using c#. in csv file we might have 2 records and 2 columns. Now i need to append one more column and value. You can write code to read the existing content from the file and append new column and data. Then, save the file with new content. Here is a simple example

How to add columns in between other columns in CSV file?

Apart from appending the columns, we will also discuss how to insert columns in between other columns of the existing CSV file. In this, we see how we make one column and add it to our CSV file but all the values in this column are the same. Open ‘input.csv’ file in read mode and create csv.reader object for this CSV file

How do I add data to a CSV file in pandas?

Pandas: How to Append Data to Existing CSV File 1 View Existing CSV File 2 Create New Data to Append 3 Append New Data to Existing CSV 4 View Updated CSV. When appending data to an existing CSV file, be sure to check whether the existing CSV has an index column or not.

What is a CSV file and how to use it?

This CSV file will contain our input data set as well as the example list as a new variable. Note that we could specify the same CSV file name within the to_csv function to overwrite our input file.


2 Answers

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

  1. the lineterminator parameter in csv.writer. By default it is set to '\r\n' 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:

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

like image 134
joaquin Avatar answered Oct 18 '22 04:10

joaquin


I'm 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. Can't 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 
like image 23
Blairg23 Avatar answered Oct 18 '22 02:10

Blairg23