简体   繁体   English

在 csv 上写入但得到没有行和列的空文件

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

I am trying to use a CSV file to store some data.我正在尝试使用 CSV 文件来存储一些数据。 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:稍后,当我读取存储变量时,命令 's' 存储它们:

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.但 CSV 文件是空的。

"I checked the caps and if the condition satisfied but didn't find anything the file even open on text editor not excel program " “我检查了大写,如果条件满足但没有找到任何文件,即使在文本编辑器上打开,也不是 excel 程序”

It is a bit unclear from your example, but I think you are closing the CSV early.从您的示例中有点不清楚,但我认为您正在提前关闭 CSV。

When you do with open('keras_character_W.csv', 'w') as csvFile: , you start a context in which:当您with open('keras_character_W.csv', 'w') as csvFile: ,您将启动一个上下文,其中:

  • The previous contents of keras_character_W.csv are erased keras_character_W.csv之前的内容被擦除
  • The file is open for writing该文件已打开写入
  • The file will be closed when you exit the with statement, meaning you stop indenting.当您退出with语句时,文件将关闭,这意味着您停止缩进。

Hope this helps!希望这可以帮助!

Keep hacking!继续黑客攻击! Keep notes.记笔记。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM