Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing to a particular cell using csv module in python

Tags:

python

csv

I have to write a value to a particular cell (say the 8th cell) in my csv file. I can see there is a csvwriter.writerow(row) method to write an entire row, but I am not seeing anything to write a value to a particular cell.

like image 534
sagar Avatar asked Sep 13 '10 11:09

sagar


People also ask

How do you write values in a CSV file in Python?

DictWriter() class can be used to write to a CSV file from a Python dictionary. Here, file - CSV file where we want to write to. fieldnames - a list object which should contain the column headers specifying the order in which data should be written in the CSV file.

Can you write directly to a CSV file in Python?

To write to a CSV file in Python, we can use the csv. writer() function. The csv. writer() function returns a writer object that converts the user's data into a delimited string.


3 Answers

The csv module provides facilities to read and write csv files but does not allow the modification specific cells in-place.

Even the csvwriter.writerow(row) method you highlight in your question does not allow you to identify and overwrite a specific row. Rather it writes the row parameter to the writer’s file object, in effect it simply appends a row the csv file associated with the writer.

Do not be dissuaded from using the csv module though, it is simple to use and with the primitives provided you could implement the higher level functionality you are looking for relatively easily.

For example take a look at the following csv file:

1,2,3,four,5
1,2,3,four,5
1,2,3,four,5

The word four is in column 3 (the fourth column but a row is just a list so the indexing is zero based), this can be easily updated to contain the digit 4 with the following program:

import csv
in_file = open("d:/in.csv", "rb")
reader = csv.reader(in_file)
out_file = open("d:/out.csv", "wb")
writer = csv.writer(out_file)
for row in reader:
    row[3] = 4
    writer.writerow(row)
in_file.close()    
out_file.close()

Resulting in the output:

1,2,3,4,5
1,2,3,4,5
1,2,3,4,5

Granted creating some generic function that allows specific rows and columns to be identified and updated is a little more work, but not much more as manipulating a csv file in Python is just manipulating a sequence of lists.

like image 56
Tendayi Mawushe Avatar answered Nov 20 '22 08:11

Tendayi Mawushe


Using xlwt module, one can perform multiple operations on spreadsheet.can write into particular or specific cell in Excel.

import xlwt 

from xlwt import Workbook
wb = Workbook() 
sheet1 = wb.add_sheet('Sheet 1') 
sheet1.write(1, 0, 'stack') 
sheet1.write(0, 1, 'overflow') 
wb.save('stackoverflow.xls') 
like image 39
siripuram Avatar answered Nov 20 '22 08:11

siripuram


suppose you have a csv file called mylist.csv with following lines:

a, b, c, d

e, f, g, h

i, j, k, l

if you want to modify 'h' to become 'X', can use this code, need to import csv module:

    f = open('mylist.csv', 'r')
    reader = csv.reader(f)
    mylist = list(reader)
    f.close()
    mylist[1][3] = 'X'
    my_new_list = open('mylist.csv', 'w', newline = '')
    csv_writer = csv.writer(my_new_list)
    csv_writer.writerows(mylist)
    my_new_list.close()

If you want to modify a particular column for each row, just add the for loop to iterate.

like image 32
Roy learns to code Avatar answered Nov 20 '22 09:11

Roy learns to code