Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overwriting a specific row in a csv file using Python's CSV module

Tags:

python

csv

I'm using Python's csv module to do some reading and writing of csv files.

I've got the reading fine and appending to the csv fine, but I want to be able to overwrite a specific row in the csv.

For reference, here's my reading and then writing code to append:

    #reading
    b = open("bottles.csv", "rb")
    bottles = csv.reader(b)
    bottle_list = []
    bottle_list.extend(bottles)
    b.close()

    #appending
    b=open('bottles.csv','a')
    writer = csv.writer(b)
    writer.writerow([bottle,emptyButtonCount,100, img])
    b.close()

And I'm using basically the same for the overwrite mode(which isn't correct, it just overwrites the whole csv file):

    b=open('bottles.csv','wb')
    writer = csv.writer(b)
    writer.writerow([bottle,btlnum,100,img])
    b.close()

In the second case, how do I tell Python I need a specific row overwritten? I've scoured Gogle and other stackoverflow posts to no avail. I assume my limited programming knowledge is to blame rather than Google.

like image 539
William Wallace Avatar asked Nov 10 '10 20:11

William Wallace


People also ask

How do I rewrite a row in a CSV file in Python?

Firstly, we have to open the file in writing “w” mode by using open() function. Then we have to create a CSV writer object by calling python built-in function writer() . Now we can write data into our CSV file by calling writerow() or writerows() functions.

How do I overwrite a csv file?

If you keep the same name on the output tool and run the workflow it will overwrite the file automatically. Hi @credlinr, when outputing a csv file with the regular output tool, it will overwrite the file without setting up anything special.

How do I edit data in a CSV file in Python?

Editing the contents of an existing CSV file will require the following steps: read in the CSV file data, edit the lists (Update information, append new information, delete information), and then write the new data back to the CSV file.


2 Answers

I will add to Steven Answer :

import csv

bottle_list = []

# Read all data from the csv file.
with open('a.csv', 'rb') as b:
    bottles = csv.reader(b)
    bottle_list.extend(bottles)

# data to override in the format {line_num_to_override:data_to_write}. 
line_to_override = {1:['e', 'c', 'd'] }

# Write data to the csv file and replace the lines in the line_to_override dict.
with open('a.csv', 'wb') as b:
    writer = csv.writer(b)
    for line, row in enumerate(bottle_list):
         data = line_to_override.get(line, row)
         writer.writerow(data)
like image 197
mouad Avatar answered Oct 19 '22 11:10

mouad


You cannot overwrite a single row in the CSV file. You'll have to write all the rows you want to a new file and then rename it back to the original file name.

Your pattern of usage may fit a database better than a CSV file. Look into the sqlite3 module for a lightweight database.

like image 29
Steven Rumbalski Avatar answered Oct 19 '22 09:10

Steven Rumbalski