Python Forum

Full Version: Write Variable String To CSV Cell
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey guys,

How would I go about writing data into a specific CSV cell?

example

x = "true"

list(csv.reader(open('test.csv', 'w')))
list.write(x)to([5][1]) #cell location row 5, column 1
hi,

this is the least best method i found for you:

import csv

f = open("mycsv.csv", "r")
reader = csv.reader(f)
mylist = list(reader)
f.close()
print(mylist)

mylist[2][0] = "x"#here 2 is third row starting from 0 and [0] is the cell postion
my_new_list = open('mylist.csv', 'w', newline = '')
csv_writer = csv.writer(my_new_list)
csv_writer.writerows(mylist)# you can use writerow also to get the data into 1 single column
my_new_list.close()
make three rows with number 1,2,3
4,5,6
7,8,9
and then use this code