1 Replies - 1293 Views - Last Post: 28 August 2012 - 02:31 AM Rate Topic: -----

#1 DoMo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 02-March 11

Writing to a CSV file through colum and row?

Posted 28 August 2012 - 02:26 AM

Hey guys, yes my second thread on the same program.

Just wondering if anybody here knows how to write user entered data (stored through raw_input) into a specified column and row combination in a .csv file?

This is what I have attempted so far.

import csv
#READS FROM THE FINAL THIRD FILE
#WRITES TO THE FINAL THIRD FILE
file1 = csv.reader(open('test1.csv', 'rb'), delimiter=',')
ofile  = open('new.csv', "wb")
writer = csv.writer(ofile, delimiter=',', quotechar='"')

studentnum = raw_input("Enter Student Number:")
col = raw_input("Enter Column:")
result = raw_input("New Result:")

acol = int(col)
aresult = int(result)

file1listnames = []
for row in file1:
    file1listnames.append(row[0])

if studentnum in file1listnames:
    for row in file1:
        row[col] = result
    writer.writerow([result])
else:
    print 'The student number %s was not found' % studentnum
ofile.close()


I this current format it will write the user result by default to A0, I have been tinkering with this bit of code though and what I thought might work was

for row in file1:
        row[col] = result
        writer.writerow(row)



But, that doesn't work, and I figured it would write that for an entire column.

SO I guess atm I am just trying to get the specific column part down, then I can move onto the row.

Any pointers / advice would be appreciated. Thankyou!

Is This A Good Question/Topic? 0
  • +

Replies To: Writing to a CSV file through colum and row?

#2 DoMo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 02-March 11

Re: Writing to a CSV file through colum and row?

Posted 28 August 2012 - 02:31 AM

Derp, I still can't find where I can edit what I posted, but I made a small error in my code

if studentnum in file1listnames:
    for row in file1:
        row[acol] = aresult
        writer.writerow(row)



There, still same problems though :S
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1