EmptyDataError while writing and reading from temporary file in Python

前提是你 提交于 2019-12-11 09:48:02

问题


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

However, codes are generating the error-EmptyDataError: No columns to parse from file.


回答1:


Try this - it will return to the top of the file:

file.seek(0)


来源:https://stackoverflow.com/questions/40774954/emptydataerror-while-writing-and-reading-from-temporary-file-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!