Python 官方文档:入门教程 => 点击学习
这篇文章主要介绍了python字节数组怎么使用的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇Python字节数组怎么使用文章都会有所收获,下面我们一起来看看吧。1、bytes和bytearray的要素都是在0-
这篇文章主要介绍了python字节数组怎么使用的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇Python字节数组怎么使用文章都会有所收获,下面我们一起来看看吧。
1、bytes和bytearray的要素都是在0-255之间的整数,但任何字符串都可以通过字符编码方案存储。字节数组切片或相应的字节数组;字节组可以直接显示ASCII字符。
s = 'helloèçí'b_arr = bytes(s, 'utf_8')print(type(b_arr))print(type(b_arr))for b in b_arr: print(b, end=' ') print()print('element of bytes is int number', b_arr[0]) print('splice of bytes is bytes',end = ' ' )b_arr_splice = b_arr[:1]print(b_arr_splice) num_b_arr = bytes([299])
2、struct模块提供了将打包的字节序列转换节序列转换成由不同类型字段组成的元组,也有一些函数用于反向转换,将元组转换成打包的字节序列。该模块可以处理bytes,bytearray和memoryview对象。
import structrecord_fORMat = 'hd4s'pack_bytes = struct.pack(record_format, 7 , 3.14,b'gbye')print(type(pack_bytes))print(pack_bytes)with open('struct.b', 'wb') as fp: fp.write(pack_bytes) record_size = struct.calcsize(record_format)with open('struct.b', 'rb') as fp: record_bs = fp.read(record_size) print(struct.unpack(record_format, record_bs))
关于“python字节数组怎么使用”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“python字节数组怎么使用”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注编程网Python频道。
--结束END--
本文标题: python字节数组怎么使用
本文链接: https://www.lsjlt.com/news/328930.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0