【发布时间】:2019-03-06 16:18:59
【问题描述】:
我有一个从套接字接收的字节对象,我想提取它包含的整数值。
看起来像这样
input = b'1 \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
我试过了
tmp_str = input.decode('ascii').strip()
int(tmp_str)
错误:
ValueError: invalid literal for int() with base 10: '1 \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
但是tmp_str的类型是'str',但是长度是20..看起来对象没有改变,只是它的一些表示改变了..
>>> print(tmp_str)
1
>>> len (tmp_str)
20
>>> type(tmp_str)
<class 'str'>
>>> type(input)
<class 'bytes'>
如何从中提取 int?
【问题讨论】:
标签: python python-3.x string int