【问题标题】:python3 renderPM (?) filehandle pb (through biopython)python3 renderPM (?) 文件句柄 pb (通过 biopython)
【发布时间】:2017-08-13 14:43:59
【问题描述】:

我想用 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。

【问题讨论】:

    标签: python python-3.x runtime-error biopython filehandle


    【解决方案1】:

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

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

    应该可以解决问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-01-02
      • 1970-01-01
      • 2014-04-18
      • 1970-01-01
      • 1970-01-01
      • 2011-09-04
      • 1970-01-01
      相关资源
      最近更新 更多