【学习笔记】Python基础 常用内建模块

来源:互联网 发布:java发牌游戏 编辑:程序博客网 时间:2024/05/21 18:43

struct

Python提供了一个struct模块来解决bytes和其他二进制数据类型的转换

struct 模块定义的数据类型可以参考Python官方文档:
https://docs.python.org/3/library/struct.html#format-characters

示例

#!/usr/bin/env python3# -*- coding: utf-8 -*-# Python基础 常用内建模块 - struct# Python提供了一个struct模块来解决bytes和其他二进制数据类型的转换import struct# >I 分别表示网络序和4字无符号整数def runTest():    result = struct.pack(">I", 1)    # b'\x00\x00\x00\x01'    print(result)    result = struct.unpack(">I", b'\x00\x00\x00\x01')    # (1,)    print(result)runTest()

运行结果

D:\PythonProject\sustudy>python main.pyb'\x00\x00\x00\x01'(1,)

具体

参考廖雪峰的教学
https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/001431955007656a66f831e208e4c189b8a9e9f3f25ba53000

这种东西容易忘记,当做工具使用即可