python 分割bytes
PHPz
PHPz 2017-04-17 15:24:02
0
2
409

我有很多bytes
比如:
b'4a\x03\x00\x00'
b'qw\x01\x00\x00'
b'zz\x02\x00\x00'
b'sf2\x03\x00\x00'
b'sd2\x05\x00\x00'
等等

这些bytes 前面是我想要的信息 后面都是诸如\x00 \x02 ....
我想要从它们从提取出 4a qw zz sf2 sd2 ...
我该怎么做呢?
我尝试过
b=b'4a\x03\x00\x00'
result=b.decode('iso-8859-1').split('\x03')[0]
这样可以分离出4a
但是有很多其他bytes,我该怎么做,才能让这样类似的bytes都分离出来

PHPz
PHPz

学习是最好的投资!

reply all(2)
阿神

This should be the answer you need. You can find ways to optimize it yourself
import string
tmp_str = string.digits + string.lowercase

src_lst = [
b'4ax03x00x00',
b'qwx01x00x00',
b'zzx02x00x00',
b'sf2x03x00x00',
b'sd2x05x00x00'
]

ret_lst = []
for tmp in src_lst:
tmp_lst = []
for tmp2 in str(tmp):
if str(tmp2) in tmp_str:
tmp_lst.append(tmp2)

ret_lst.append(tmp_lst)

print ret_lst

迷茫

Str it first and then process it.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!