【问题标题】:Why the result prints b'hello,Python!' ,when I use tensorflow? [duplicate]为什么结果打印 b'hello,Python!' ,当我使用张量流? [复制]
【发布时间】:2018-03-08 10:40:08
【问题描述】:

代码固定在下面:

import tensorflow as tf

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

sess=tf.Session()

print(sess.run(hello))

当前结果固定在下方:

b'你好,Python!'

然后截图

那么,我应该怎么做才能在当前结果之前删除奇怪的“b”?

【问题讨论】:

  • 我的描述可能有点令人费解,其实我跑完代码后,结果的头部显示了一个“b”,让我很困惑。
  • b 字符前缀表示Hello, TensorFlow!byte string

标签: python tensorflow


【解决方案1】:

根据 Python 2.x documentation:

'b' 或 'B' 前缀在 Python 2 中被忽略;它表明 文字应该成为 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!'

【讨论】:

  • 这是一个临时解决方案。所以,每次都需要换。有什么永久的解决方案吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多