Hassan_mohammad
Hassan_mohammad

Reputation: 83

write on csv but getting empty file with no row and columns

I am trying to use a CSV file to store some data. This code creates the file:

with open('keras_character_W.csv', 'w') as csvFile:
    fieldnames = ['images', 'result_character']
    writer = csv.DictWriter(csvFile, fieldnames=fieldnames)
    writer.writeheader()

later, when I am read to store the variables, a command 's' stores them:

state = cv2.waitKey(0)
if state == ord('q'):
   break

elif state == ord('s'):
     writer.writerow({'images': '  '})
     writer.writerow({'images': 'guess_right = ', 'result_character': x, })
     writer.writerow({'images': 'final  = ', 'result_character': y , })

but the CSV file is empty.

"I checked the caps and if the condition satisfied but didn't find anything the file even open on text editor not excel program "

Upvotes: 0

Views: 119

Answers (1)

Charles Merriam
Charles Merriam

Reputation: 20500

It is a bit unclear from your example, but I think you are closing the CSV early.

When you do with open('keras_character_W.csv', 'w') as csvFile:, you start a context in which:

  • The previous contents of keras_character_W.csv are erased
  • The file is open for writing
  • The file will be closed when you exit the with statement, meaning you stop indenting.

Hope this helps!

Keep hacking! Keep notes.

Upvotes: 1

Related Questions