python3 renderPM (?) 文件句柄 pb(通过 biopython)

python3 renderPM (?) filehandle pb (through biopython)

我想用 biopython 模块 Bio/Graphics/BasicChromosome.py 生成核型图像。 这是我的代码

import sys
import pprint # debug
from Bio.Graphics import BasicChromosome


diagram = BasicChromosome.Organism()
diagram.output_format = "pdf"

# Ok, it works
# diagram.draw( "toto.pdf", "test" )

# this one fail.
with open( "toto.pdf", 'w' ) as o_out:
    diagram.draw( o_out, "test" )

此脚本生成一个空图表,pdf 文档中只有标题 "test"。至少它应该,实际上它确实与 python2.

使用 python3,它失败并出现以下错误:

Traceback (most recent call last):
  File "test_emptyKaryo.py", line 16, in <module>
    diagram.draw( o_out, "test" )
  File "/usr/lib/python3/dist-packages/Bio/Graphics/BasicChromosome.py", line 161, in draw
    return _write(cur_drawing, output_file, self.output_format)
  File "/usr/lib/python3/dist-packages/Bio/Graphics/__init__.py", line 82, in _write
    return drawmethod.drawToFile(drawing, output_file)
  File "/usr/lib/python3/dist-packages/reportlab/graphics/renderPDF.py", line 298, in drawToFile
    c.save()
  File "/usr/lib/python3/dist-packages/reportlab/pdfgen/canvas.py", line 1237, in save
    self._doc.SaveToFile(self._filename, self)
  File "/usr/lib/python3/dist-packages/reportlab/pdfbase/pdfdoc.py", line 222, in SaveToFile
    f.write(data)
TypeError: write() argument must be str, not bytes

似乎不​​再支持文件句柄。 我不认为这是一个 biopython 错误。 任何人都可以给我一个线索让它起作用吗?

如果我把输出格式改成png,pdf就不再涉及了,所以我认为它来自renderPM.py。

您需要将字符串对象传递给以 string 模式打开的文件对象或以 binary 模式打开输出文件(在这种情况下更容易)。

with open("toto.pdf", 'wb') as o_out:

应该可以解决问题。