当我使用tensorflow时,为什么结果打印为b'hello,Python!'?

3
代码固定在下面:
import tensorflow as tf

hello=tf.constant("hello,Python!")

sess=tf.Session()

print(sess.run(hello))

enter image description here

当前结果如下所示:

b'hello,Python!'

然后是截图。

那么,我应该如何去掉当前结果前的奇怪的“b”呢?


我的描述可能有点令人困惑,实际上,在我运行代码后,结果的开头显示了一个“b”,这让我非常困惑。 - DawnZhang
字符前缀b表示Hello, TensorFlow!字节串 - call-in-co
1个回答

6
根据 Python 2.x 的文档:链接
在 Python 2 中,前缀 'b' 或 'B' 会被忽略,它表示字面值应该在 Python 3 中变成 bytes 字面值(例如,当代码使用 2to3 自动转换时)。'u' 或 'b' 前缀后面可以跟一个 'r' 前缀。
所以在 Python 3.x 中:
bytes = b'...' 字面值是一个八位字节序列(介于 0 和 255 之间的整数)。
它不是真正存在的,只是被打印出来。这不应该在任何地方引起问题。
你可以尝试使用 decode("utf-8") 将其转换为 str。
对我有效。
>>> print(out)
b'hello,Python!'
>>> out.decode('utf-8')
'hello,Python!'

这是一个临时解决方案。因此,每次都需要更改它。有没有永久性的解决方案? - Emalka

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接