如何解决读取.bed文件并以特定格式压缩输出时出现问题
import os #handles the gzipped output like the example file
file_name = "exampleziptotxt.bed"
out_file_root = "example_by_chrom"
file_handle_dict = {}
file_reader=open(file_name)
for line in file_reader:
ff=line.strip().split(",")
chrom_name=ff[0]
if not (chrom_name in file_handle_dict):
out_file_chrom_name=out_file_root+"."+chrom_name+".bed"
out_file_chrom_name_handle=open(out_file_chrom_name,"w")
file_handle_dict[chrom_name]=out_file_chrom_name_handle
# write the line in the appropriate output file
file_handle_dict[chrom_name].write(line)
# file_handle_dict[chrom_name].write("%s\n"%"\t".join(ff))
file_reader.close()
# now close all open files
for chrom_name in file_handle_dict:
file_handle_dict[chrom_name].close()
我想重写上面的代码,以便使用gzip或替代方法输出多个gzip压缩文件。我不确定如何做到这一点。任何帮助将不胜感激。
解决方法
只需进行一些小更改,import gzip
(不需要os
),将.gz
添加到名称,使用gzip.open
代替{{1} }进行写入,并在该行上使用open
使其变为字节。
.encode()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。