Hi,
I am using a csv module to read a spreadsheet. Now what I want to do is change one of the rows in the spreadsheet. I do not want the same row repeated again when I csv.writer to write a row. I simply want to read a row from csv file, change a value in that row and write it back into the spreadsheet without repeating it in the spreadsheet. Partial code is below. Initially the csv file contains a row which is read as ['A', '1'] and I want to change it to ['A', '3']. But of course I want to get rid of the row ['A', '1'] initially there.
self._file = open("test.csv", "r+")
self._filereader = csv.reader(self._file)
self._filewriter = csv.writer(self._file)
for row in self._filereader:
if row[0] == 'A':
self._newrow = ['A', '3']
self._filewriter.writerow(self._newrow)
self._file.close()
I am using a csv module to read a spreadsheet. Now what I want to do is change one of the rows in the spreadsheet. I do not want the same row repeated again when I csv.writer to write a row. I simply want to read a row from csv file, change a value in that row and write it back into the spreadsheet without repeating it in the spreadsheet. Partial code is below. Initially the csv file contains a row which is read as ['A', '1'] and I want to change it to ['A', '3']. But of course I want to get rid of the row ['A', '1'] initially there.
self._file = open("test.csv", "r+")
self._filereader = csv.reader(self._file)
self._filewriter = csv.writer(self._file)
for row in self._filereader:
if row[0] == 'A':
self._newrow = ['A', '3']
self._filewriter.writerow(self._newrow)
self._file.close()