Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python 3.5中写入Gzip格式的fasta文件_Python_Python 3.5_Biopython - Fatal编程技术网

在python 3.5中写入Gzip格式的fasta文件

在python 3.5中写入Gzip格式的fasta文件,python,python-3.5,biopython,Python,Python 3.5,Biopython,我刚刚将工具中的所有模块从Python2迁移到Python3。我遇到了一个我无法解决的问题——我不知道如何写入Gzip文件 with gzip.open("sample.fasta.gz", "w") as file: print("writing...") for oid, seq in temp_data.items(): # prepare row row = SeqRecord(Seq(seq), id=str(oid), descript

我刚刚将工具中的所有模块从Python2迁移到Python3。我遇到了一个我无法解决的问题——我不知道如何写入Gzip文件

with gzip.open("sample.fasta.gz", "w") as file:
    print("writing...")
    for oid, seq in temp_data.items():
        # prepare row
        row = SeqRecord(Seq(seq), id=str(oid), description=temp_tax[oid])
        SeqIO.write(row, file, "fasta")
此代码适用于python 2,但不适用于python 3,它会引发:

TypeError:memoryview:需要类似字节的对象,而不是“str”


如何解决此问题?

尝试将从
SeqRecord
返回的字符串转换为
字节
对象

byte_row = record.format('').encode()
字符串的形式返回记录

字符串
转换为
字节
对象。

与常规的
打开
函数不同,默认为二进制模式,而不是文本模式。您必须将
t
作为模式字符串的一部分显式传递,以在文本模式下打开(该模式接受/期望
str
):

byte_row = record.format('').encode()

为了避免行结束转换,您可能还希望(空字符串),以便在Windows这样的系统上,它在写入时不会将
\n
转换为
\r\n

我完全忘记了
t
选项。谢谢