在Python中写入和读取临时文件时出现EmptyDataError [英] EmptyDataError while writing and reading from temporary file in Python
本文介绍了在Python中写入和读取临时文件时出现EmptyDataError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在通过创建一个临时中间文件将一种文件格式转换为另一种文件格式.以下是我的代码:
I am converting one file format into another file format by creating a temporary intermediate file. Following are my codes:
import tempfile
import pandas as pd
from pandas import DataFrame
def convert_binary_step1(file1,file2,file3):
source=open(file1)
SampleList=convert_file_to_list(file2)
dest=open(file3,'w')
ti= tempfile.NamedTemporaryFile(delete=False)
l=[]
for line in source:
a=line.split()
del a[-1]
for i in range(len(SampleList)):
if SampleList[i] in a:
l.append("1")
else:
l.append("0")
data=a[0]+"\t"+"\t".join(l)+"\n"
ti.write(data.encode("utf-8"))
del l[:]
source1=pd.read_csv(ti,sep="\t",encoding='utf-8',header=None)
dest=source1.transpose()
dest.to_csv(file3,sep="\t",header=None,index=None)
source.close()
dest.close()
ti.close()
def convert_file_to_list(file2):
source11=open(file2)
li=[]
for lin in source11:
b=lin.split()
li+=[b[0]]
return li
source11.close()
但是,代码正在生成错误- EmptyDataError:没有要从文件中解析的列.
However, codes are generating the error-EmptyDataError: No columns to parse from file.
推荐答案
尝试一下-它会返回文件的顶部:
Try this - it will return to the top of the file:
file.seek(0)
这篇关于在Python中写入和读取临时文件时出现EmptyDataError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文