How to add a new column to the beginning of a CSV file?

I have one CSV file in which I have 6 to 8 columns.
Example:

ID Test Description file-name module view path1 path2 

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

 Node ID Test Description file-name module view path1 path2 
+4
source share
2 answers

It would be fairly easy to do using the csv classes DictReader and DictWriter . Here is an example that reads an old file and writes a new one in one go.

A DictReader instance returns each logical line or line in a file as a dictionary whose keys are field names. You can explicitly specify the field names or you can read them from the first line of the file, as shown in the example.

When creating a DictWriter instance DictWriter you must specify the required field names, and the order of the field names determines the order that they will be displayed on each line of the output file. In this case, the new field name is simply added to the top of the list of names from the input file - whatever they are.

 import csv with open('testdata.txt', 'rb') as inf, open('testdata2.txt', 'wb') 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, 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 will 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 data for a field to each row using dict(row, Node='node %s' % node) , as shown, only works when the field name is a valid keyword argument (i.e. a valid Python identifier) - like Node .

Valid identifiers consist only of letters, numbers, and underscores, but do not start with a number or underscore and cannot be a language keyword such as class, for, return, global, pass, print (in Python 2) or boost.

To get around this limitation, you must do this separately:

  for node, row in enumerate(csvreader, 1): row['Invalid Keyword'] = 'node %s' % node # add new field and value csvwriter.writerow(row) 
+14
source

You can use the CSV module to read in your CSV file and write out the edited version with the added column. Remember that adding a column adds an extra record to the end of each row.

Example output using 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']) 
+4
source

Source: https://habr.com/ru/post/1338058/


All Articles