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

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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值