python - 在追加文件时,数据没有写入文件,而在读取文件时,它在 python 中显示空字节

标签 python linux file error-handling

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

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()

如有任何帮助,我们将不胜感激。

最佳答案

您可以检查的几件事:

  1. 变量 v1、op_minute 已分配给它们正确的值
  2. 检查您是否能够在不附加任何内容的情况下读取文件的当前内容(如果文件已经包含一些数据)。分隔符可能存在一些问题。

我尝试运行您的代码,它对我来说运行良好。

关于python - 在追加文件时,数据没有写入文件,而在读取文件时,它在 python 中显示空字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48822567/

相关文章:

Linux 不显示添加的组

linux - 我如何使用 perl cd 进入一个目录?

linux - pthread_cond_broadcast 问题

javascript - PDFkit js如何将文档保存到文件(Win 8应用程序)

python - 读取和写入同一个文本文件

ios - 将自定义对象写入和读取文件 IOS

Python - 为 raise exception_class(message, screen, stacktrace) 进行异常处理

python - 为什么有关 python 命令行启动的记录没有保存在历史记录中

python - 如何在 ModelForm 中使用 forms.ChoiceField()?

python - 值错误: invalid literal for int() with base 10: '–20'