在附加文件时,数据不会写入文件,而在读取时,在python中显示空字节

2024-03-29 05:55:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用文件处理的append方法在文件中写入数据,但它没有显示在文件中。 读取文件时也会显示空字节错误

with open("filename.txt",'a') as f_ro_panel:
    f_ro_panel.write('\n'+str(vl)+'\t'+str(op_minute)+'\t'+str(bcc)+\
           '\t'+str(temp)+'\t'+str(tds2)+'\t'+str(tds1)+'\t'+str(pfr)+\
           '\t'+str(rfr)+'\t'+str(rwpc)+'\t'+str(hppc)+'\t'+str(twv)+\
           '\t'+str(rwv)+'\t'+str(uvst)+'\t'+str(tsc)+'\t'+str(tlv))
    f_ro_panel.close()

这是写作的准则

在阅读时,我正在使用这个程序

with open("filename.txt", 'rU') as my_file:
    reader = csv.reader(my_file, delimiter='\t')
    my_list = list(reader)
    ListLen = len(my_list)
    my_file.close()

任何帮助都将不胜感激


Tags: 文件txtcloseromyaswithopen
1条回答
网友
1楼 · 发布于 2024-03-29 05:55:32

您可以检查以下几项:

  1. 变量v1,op\u minute有正确的赋值
  2. 检查是否能够读取文件的当前内容而不附加任何内容(如果文件已包含某些数据)。分隔符可能有问题

我试着运行你的代码,它对我来说很好

相关问题 更多 >