import gzip
input_file = open("example.bed","rb")#compress existing file
data = input_file.read()
with gzip.open("example.bed.gz", "wb") as filez:
filez.write(data)
filez.close()
fileopen= gzip.open("example.bed.gz", "r+")
output=fileopen.read()
decode=output.decode("utf-8")
import pandas as pd
df=pd.read_csv(decode, delimiter='\t',header=1 )#Works partially, missing '\t' at start of file throws error
df.to_csv('exampleziptotxt.bed', index=False)
使用熊猫将压缩.bed
文件转换为.txt
文件。文件的开头是chr8\t..
。因为没有引导\t
FileNotFoundError:[Errno 2]返回文件chr8。任何建议如何纠正输入文件,使其包含前导\t
?
read_csv()
接受文件名或类似文件的对象。
您正在将文件的内容作为字符串传递给它,它试图将其解释为文件名,并尝试打开名为的内容chr8
,但失败。
相反,只需将gzip文件句柄传递给函数(因为它是一个类似文件的对象):
import pandas as pd
with gzip.open("example.bed.gz", "r+") as fileopen:
df = pd.read_csv(fileopen, delimiter="\t", header=1)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句