英文:
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:
>>> 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
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论