首页 > 解决方案 > 在 Google Colab 中查看字符串输出时出现问题

问题描述

在此处输入图像描述

当我运行命令'print(abide.description)'时,我应该获得这样的输出,但我获得的输出是这样的。整个字符串显示在一行中,这使得阅读和解释非常困难。如何获得如上图的输出?

我的代码片段:

print(abide.description)

输出: 在此处输入图像描述

标签: pythonstringviewgoogle-colaboratoryabide

解决方案


问题是abide.description返回字节而不是字符串。如果您希望它作为普通字符串打印,您可以使用该bytes.decode()方法将字节转换为 unicode 字符串。

例如:

content_bytes = b'this is a byte string\nand it will not be wrapped\nunless it is first decoded'

print(content_bytes)
# b'this is a byte string\nand it will not be wrapped\nunless it is first decoded'

print(content_bytes.decode())
# this is a byte string
# and it will not be wrapped
# unless it is first decoded

推荐阅读