python - 在 Google Colab 中查看字符串输出时出现问题
问题描述
当我运行命令'print(abide.description)'时,我应该获得这样的输出,但我获得的输出是这样的。整个字符串显示在一行中,这使得阅读和解释非常困难。如何获得如上图的输出?
我的代码片段:
print(abide.description)
解决方案
问题是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
推荐阅读
- docker - 如何从 docker 访问 WAMP 虚拟主机地址?
- java - 添加外部 Jar 不起作用,出现 NoClassDefFoundError
- quarkus - Quarkus 应用程序可以连接到多个 JMS 代理实例吗?
- string - Powershell Get-History to String
- ios - 多参数字典(集合列表) 喜欢:[[String: Any]] 到 Alamofire 参数
- sql - 如何将 SYSDATE 提前 10 年设置为默认日期 12 月 31 日?甲骨文数据库
- javascript - Featherlight lightbox 与 Spring 和 Thymeleaf 一起无法在框中打开图像
- microsoft-graph-api - Microsoft Graph API:事件还原期间的“ErrorDuplicateTransactionId”
- spring - 无法为 MongoDB 指定用户名和密码
- python - TensorFlow 2.x:使用嵌入列时无法加载 h5 格式的训练模型(ValueError:Shapes (101, 15) 和 (57218, 15) 不兼容)