Jagraj Singh
Jagraj Singh

Reputation: 19

Why is my CSV File not being printed?

My code currently writes a dictionary which contains scores for a class to a CSV file. This part is correctly done by the program and the scores are wrote to file, however the latest dictionary written to file is not printed. For example, after the code has been ran once, it will not be printed however once the code has been ran for a second time, the first bit of data is printed however the new data isn't. Can someone tell me where I am going wrong?

SortedScores = sorted(Class10x1.items(), key = lambda t: t[0], reverse = True) #this sorts the scores in alphabetical order and by the highest score
FileWriter = csv.writer(open('10x1 Class Score.csv', 'a+'))
FileWriter.writerow(SortedScores) #the sorted scores are written to file     
print "Okay here are your scores!\n"

I am guessing the problem is here somewhere however I cannot quite pinpoint what or where it is. I have tried to solve this by changing the mode of the file when it is read back in to r, r+ and rb, however all have the same consequence.

ReadFile = csv.reader(open("10x1 Class Score.csv", "r")) #this opens the file using csv.reader in read mode
for row in ReadFile:
    print row
return

Upvotes: 0

Views: 512

Answers (2)

Tadhg McDonald-Jensen
Tadhg McDonald-Jensen

Reputation: 21453

from Input output- python docs:

It is good practice to use the with keyword when dealing with file objects. This has the advantage that the file is properly closed after its suite finishes, even if an exception is raised on the way. It is also much shorter than writing equivalent try-finally blocks: >>> with open('workfile', 'r') as f: ... read_data = f.read() >>> f.closed True File objects have some additional methods, such as isatty() and truncate() which are less frequently used; consult the Library Reference for a complete guide to file objects.

I'm not sure why they bury that so far in the documentation since it is really useful and a very common beginner mistake:

SortedScores = sorted(Class10x1.items(), key = lambda t: t[0], reverse = True) #this sorts the scores in alphabetical order and by the highest score
with open('10x1 Class Score.csv', 'a+') as file:
    FileWriter = csv.writer(file)
    FileWriter.writerow(SortedScores) #the sorted scores are written to file     
print "Okay here are your scores!\n"

this will close the file for you even if an error is raised which prevents many possibilities of loss of data

the reason it did not appear to write to the file is because when you do .write_row() it doesn't immediately write to the hard drive, only to a buffer which is occasionally emptied into the file on hard drive, although with only one write statement it has no need to empty.

Upvotes: 1

kagami
kagami

Reputation: 611

Remember to close the file after operation, otherwise the data will not be saved properly.

Try to use with keyword so that Python will handle the closure for you:

import csv

with open('10x1 Class Score.csv', 'a+') as f:
    csv_writer = csv.writer(f)
    # write something into the file
    ...
# when the above block is done, file will be automatically closed
# so that the file is saved properly

Upvotes: 0

Related Questions