Every print to stdout fails because write claims its input is bytes, not string

huangapple go评论48阅读模式
英文:

Every print to stdout fails because write claims its input is bytes, not string

问题

I'm trying to debug a module, but whenever I try to print something, python 3.10 stops with the following error:

File "/usr/lib/python3.10/codecs.py", line 378, in write
self.stream.write(data)
TypeError: write() argument must be str, not bytes

This error comes whatever I am trying to print, e.g. print("abc") or print(type(1)). It also occurs when calling breakpoint(), because breakpoint prints its messages to stdout, which does not work for some reason.

When calling print in a separate python shell, it works, but not when it is called from the module which is loaded in a script.

Any ideas what might cause this behavior?

Have tried to actually print the data type with print(type(var)) and print(isinstance(var,bytes)), but this results in the same error.

英文:

I'm trying to debug a module, but whenever I try to print something, python 3.10 stops with the following error:

File "/usr/lib/python3.10/codecs.py", line 378, in write
self.stream.write(data)
TypeError: write() argument must be str, not bytes

This error comes whatever I am trying to print, e.g. print("abc") or print(type(1)). It also occurs when calling breakpoint(), because breakpoint prints its messages to stdout, which does not work for some reason.

When calling print in a separate python shell, it works, but not when it is called from the module which is loaded in a script.

Any ideas what might case this behaviour?

Have tried to actually print the data type with print(type(var)) and print(isinstance(var,bytes)), but this results in the same error.

答案1

得分: 1

string_data = data.decode('utf-8')

stream.write(string_data)

英文:
data = b'abc'

string_data = data.decode('utf-8')

stream.write(string_data)

答案2

得分: 0

这个问题通常发生在你尝试设置标准输出(stdout)的默认编码时,就像在Python 2.x中所做的那样:

>>> import sys; import codecs
>>> sys.stdout = codecs.getwriter("utf-8")(sys.stdout)
>>> print("bla")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.5/codecs.py", line 377, in write
    self.stream.write(data)
TypeError: write() argument must be str, not bytes
>>> print(type("bla"))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.5/codecs.py", line 377, in write
    self.stream.write(data)
TypeError: write() argument must be str, not bytes

尽管在Python 3.x中有其他方法来实现这个目标,但这可能会在打印不表示字符串的二进制数据时导致意外的副作用。因此,最安全的做法可能是不使用强制解码方法,而是在实际需要时显式调用decode

英文:

This problem typically occurs when you have tried to set the default encoding of stdout like it was done in python 2.x:

&gt;&gt;&gt; import sys; import codecs
&gt;&gt;&gt; sys.stdout = codecs.getwriter(&quot;utf-8&quot;)(sys.stdout)
&gt;&gt;&gt; print(&quot;bla&quot;)
Traceback (most recent call last):
  File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;
  File &quot;/usr/lib/python3.5/codecs.py&quot;, line 377, in write
   self.stream.write(data)
TypeError: write() argument must be str, not bytes
&gt;&gt;&gt; print(type(&quot;bla&quot;))
Traceback (most recent call last):
  File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;
  File &quot;/usr/lib/python3.5/codecs.py&quot;, line 377, in write
  self.stream.write(data)
TypeError: write() argument must be str, not bytes

Although there are other ways to achieve this in Python 3.x, this might lead to unintended side-effects when printing binary data that does not represent strings. It might thus be safest not to use a forced decoding method, but to explicitly call decode when it is actually needed.

huangapple
  • 本文由 发表于 2023年6月22日 18:29:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76530958.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定