python实现:处理b'\xab\xab\x02\x00\x04\x00\x01\x03\x01\x00\x01数据,从中提取2,0,4等数字
时间: 2023-11-02 19:07:44 浏览: 136
可以使用Python的bytes类型的decode方法将字节串转换为字符串,然后使用正则表达式提取数字。具体实现如下:
```python
import re
data = b'\xab\xab\x02\x00\x04\x00\x01\x03\x01\x00\x01'
data_str = data.decode('latin1')
numbers = re.findall(r'\d+', data_str)
print(numbers) # ['2', '0', '4']
```
相关问题
python实现:按“\x”分割b'\xab\xab\x02\x00\x04\x00\x01\x03\x01\x00\x01数据,并将数据存储成list
可以使用 Python 的 `split()` 方法来按照指定的分隔符分割字符串,但是需要先将字节串转换为字符串。可以使用 `decode()` 方法将字节串转换为字符串,然后再使用 `split()` 方法进行分割。代码如下:
```python
byte_str = b'\xab\xab\x02\x00\x04\x00\x01\x03\x01\x00\x01'
str_data = byte_str.decode('utf-8')
data_list = str_data.split('\\x')
```
这样就可以得到一个包含分割后数据的列表 `data_list`。
python实现:按“\x0”分割b'\xab\xab\x02\x00\x04\x00\x01\x03\x01\x00\x01数据,并将数据进行存储
可以使用 Python 的 split() 方法来按照指定的分隔符分割字符串。但是,由于这里的数据是二进制数据,我们需要先将其转换为字符串,再进行分割。具体实现如下:
```python
data = b'\xab\xab\x02\x00\x04\x00\x01\x03\x01\x00\x01'
data_str = ''.join([chr(byte) for byte in data])
split_data = data_str.split('\x0')
```
这里,我们先使用列表推导式将二进制数据转换为字符串,然后使用 split() 方法按照 '\x0' 分割字符串。最终得到的 split_data 就是分割后的数据列表。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)