【问题标题】:inkex.py TypeError: write() argument must be str, not bytesinkex.py TypeError: write() 参数必须是 str,而不是字节
【发布时间】:2020-12-03 21:57:11
【问题描述】:

我正在尝试解决与 Inkscape 一起分发的 inkex.py 中的函数中的问题。功能是

        def output(self):
        """Serialize document into XML on stdout"""
        original = etree.tostring(self.original_document)        
        result = etree.tostring(self.document)        
        if original != result:
            self.document.write(sys.stdout)

错误消息是“TypeError:write() 参数必须是 str,而不是字节”。我认为这是Python 3 TypeError: must be str, not bytes with sys.stdout.write() 中讨论的编码问题,但我不明白输入是如何传递到这里的,因为我在self.document.write(sys.stdout) 中看不到sys.stdout 的任何输入。原始源代码可能是为 Python 2 编写的,但我的系统中有 Python 3。

这用于在inkscape程序中分割文本,完整的错误信息如下:

    Traceback (most recent call last):
    File "split.py", line 218, in <module>
    b.affect()
    File "C:\Program Files\Inkscape\share\extensions\inkex.py", line 285, in affect
    self.output()
    File "C:\Program Files\Inkscape\share\extensions\inkex.py", line 272, in output
    self.document.write(sys.stdout)
    File "src\lxml\etree.pyx", line 2057, in lxml.etree._ElementTree.write
    File "src\lxml\serializer.pxi", line 758, in lxml.etree._tofilelike
    File "src\lxml\etree.pyx", line 318, in lxml.etree._ExceptionContext._raise_if_stored
    File "src\lxml\serializer.pxi", line 682, in lxml.etree._FilelikeWriter.write
    TypeError: write() argument must be str, not bytes

【问题讨论】:

  • 要么使用 Python2 系统来运行您的 Py2 代码(不推荐,因为今年早些时候 Python 2 即将停产),因此请考虑升级到 Python3。或者,将sys.stdout 解码为关闭类型str 而不是bytes,因为这就是python 系统将其解释为的内容。提示:检查您的数据类型。
  • 更新也可以工作......你的 Inkscape 版本是什么?您是否检查过官方存储库中的 Inkscape 扩展是否已修复此问题? (很可能是)

标签: python inkscape


【解决方案1】:

我使用的是 Inkscape 0.92 版,我不知道之后的版本,但最近发布了 1.0。

但是,我通过更改以下行解决了问题

if original != result:
            self.document.write(sys.stdout)

if original != result:
            self.document.write(sys.stdout.buffer)

【讨论】:

  • 这个也修复了我!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-17
  • 2019-03-15
  • 2018-04-12
  • 2016-11-26
相关资源
最近更新 更多