Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

The csv.writer object is an improved version of this simple code:

data = [('Bus Number', 'MW'),
        ('3', '20.4'),
        ('4', '20.5')]

csvfile = open('generation.csv', 'wb')
for row in data:
    line = ','.join(row)
    csvfile.write(line + '\n')

See how it writes each row line by line? There isn't a way to change one character without re-writing the whole file.

The way to change a particular cell

Change the cell in your data variable. For example, you'd like to change B3 in the data variable above from '20.5' to '20.6'. Then re-write the file.

# row 3, column 2
data[2][1] = '20.6'

writer = csv.writer(open('generation.csv', 'wb'))
writer.writerows(data)