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:
- read in existing file
- alter file in memory
- 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?"