Skip to content Skip to sidebar Skip to footer

How Can I Replace A Value In An Existing Excel Csv File Using A Python Program?

How can I update a value in an existing .csv file using a python program. At the moment the file is read into the program but I need to be able to change this value using my progra

Solution 1:

import csv
r = csv.reader(open('productcodes.csv'))
lines = [l for l in r]
for l in lines:
    l[1] = "new value"

writer = csv.writer(open('productcodes.csv', 'w'))
writer.writerows(lines)

You can't really replace values in the existing file. Instead, you need to:

  1. read in existing file
  2. alter file in memory
  3. write out new file(overwriting existing file)

Post a Comment for "How Can I Replace A Value In An Existing Excel Csv File Using A Python Program?"