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

Daw*_*ang 3 python tensorflow

代码固定在下面:

import tensorflow as tf

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

sess=tf.Session()

print(sess.run(hello))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

当前结果固定在下面:

“你好,Python!”

然后是截图

那么,我该怎么做才能去掉当前结果之前奇怪的“b”呢?

Ama*_*ngh 6

根据 Python 2.x文档

Python 2 中会忽略前缀“b”或“B”;它表明该文字应该成为 Python 3 中的字节文字(例如,当代码使用 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!'
Run Code Online (Sandbox Code Playgroud)