Python :将编码为字节字符串(来自 PyTorch)的浮点数转换为 int

Python : convert a float encoded as a byte string (from PyTorch) to an int

我使用 .detach().numpy() 转换了 PyTorch 的输出,它产生了这种数据:

b'0.06722715'

根据 Python 的 type(),这是一个 byte 类型。如何将其转换为整数?

试试这个(在代码注释中解释)。您可以将 0.06 转换为整数,但您会得到一个零。您是指浮动吗?

#byte
b = b'0.06722715'
# to string
s = b.decode()
# to float
f = float(s)
# to integer
i = int(f)
print("Float", f)
print("Integer", i)

或者干脆

be_float = float(b.decode())
print (be_float)