python strtuct模块主要在Python中的值于C语言结构之间的转换。可用于处理存储在文件或网络连接(或其它来源)中的二进制数据。

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time : 2019/07/22 23:57
# @Author : wang huixi
# @File : update.py
import os import struct '''
数据格式
名字 职业 年
muyu coder 2018
''' name = b'muyu'
job = b'coder'
year = 2018 file = open(r'1.txt', 'rb+') file.write(struct.pack('4s5si', name, job, year))
file.flush() file.seek(0) strBin = file.read()
print(strBin) # b'muyucoder\x00\x00\x00\xe2\x07\x00\x00' content = struct.unpack('4s5si', strBin)
print(content) # (b'muyu', b'coder', 2018)
print struct.pack('>I', 10240099)

struct 最常用的方法有两个:
struct.pack(fmt,v1,v2,…)
返回的是一个字符串,是参数按照fmt数据格式组合而成
struct.unpack(fmt,string)
按照给定数据格式解开(通常都是由struct.pack进行打包)数据,返回值是一个tuple(元组)

Python的Struct模块的更多相关文章

  1. Python学习——struct模块的pack、unpack示例

    he struct module includes functions for converting between strings of bytes and native Python data t ...

  2. c语言write与python的struct模块交互

    以下讲的都是用二进制形式打开文件.网上有很多struct模块的文章,下面是我做的小实验. 1.对于c里面的fwrite写入一个单字节,写的就是它的二进制.如3,写入文件就是二进制0x03,它并不是3的 ...

  3. python中struct模块及packet和unpacket

    转自:http://www.cnblogs.com/gala/archive/2011/09/22/2184801.html 我们知道python只定义了6种数据类型,字符串,整数,浮点数,列表,元组 ...

  4. 【转】在Python的struct模块中进行数据格式转换的方法

    这篇文章主要介绍了在Python的struct模块中进行数据格式转换的方法,文中还给出了C语言和Python语言的数据类型比较,需要的朋友可以参考下 Python是一门非常简洁的语言,对于数据类型的表 ...

  5. day30 python学习 struct模块和 subprocess 模块

    import subprocess import struct aa=input('>>') obj=subprocess.Popen(aa,shell=True,#aa代表的是读取字符串 ...

  6. Python:struct模块的pack、unpack

    mport struct pack.unpack.pack_into.unpack_from 1 # ref: http://blog.csdn<a href="http://lib. ...

  7. python中struct模块

    # #********struct模块********# # 1.按照指定格式将Python数据转换为字符串,该字符串为字节流,如网络传输时, # 不能传输int,此时先将int转化为字节流,然后再发 ...

  8. Python之struct模块

    面对网络协议,在组包拆包时,python提供了struct模块,它可以帮助我们在python值和C语言的结构体之间相互转换,下面一起来了解struct的具体用法. 假设,我们的网络协议为消息id(un ...

  9. Python struct模块

    有的时候需要用python处理二进制数据,比如,存取文件,socket操作时.这时候,可以使用python的struct模块来完成.可以用 struct来处理c语言中的结构体. struct模块中最重 ...

随机推荐

  1. postgreSQL 之 Privilege & grant & revoke(未完待续)

    When an object is created, it is assigned an owner. The owner is normally the role that executed the ...

  2. python多线程、线程锁

    1.python多线程 多线程可以把空闲时间利用起来 比如有两个进程函数 func1.func2,func1函数里使用sleep休眠一定时间,如果使用单线程调用这两个函数,那么会顺序执行这两个函数 也 ...

  3. HANA到MySQL数据同步方法!

    随着各行各业信息化建设的不断发展,异构数据库间的互通.汇聚,挖掘,分析逐渐被提上日程, TreeSoft数据库管理系统,实现了异构数据库的维护.监控.可视化.自动交换同步.目前支持MySQL,Orac ...

  4. linux下maven环境的搭建

    1.maven的下载 2.maven的安装和环境变量配置 系统环境linux centos7.2 x64 1.maven的下载 下载地址:https://mirrors.tuna.tsinghua.e ...

  5. ip routing 开启三层路由模式

    no ip router是关闭路由协议,no ip routing 是关闭三层的路由工作模式 no ip route是删除某条(静态)路由,比如no ip router 0.0.0.0 0.0.0.0 ...

  6. 软件测试工具LoadRunner常见问题

    1.LoadRunner录制脚本时为什么不弹出IE浏览器? 当一台主机上安装多个浏览器时,LoadRunner录制脚本经常遇到不能打开浏览器的情况,可以用下面的方法来解决. 启动浏览器,打开Inter ...

  7. Meta Post

    $\require{color} \require{enclose}$ 写博客的一些技巧和工具. To use the following three macros it is necessary t ...

  8. mysql 字段定义不要用null的分析

    一 NULL 为什么这么经常用 (1) java的null null是一个让人头疼的问题,比如java中的NullPointerException.为了避免猝不及防的空指针,需要小心翼翼地各种if判断 ...

  9. SpringBoot导入mail依赖报错

    报错:Missing artifact org.springframework.boot:spring-boot-starter-mail:jar:2.0.3 之前导入log4j时报的一样的错误,最后 ...

  10. 剑指offer43:左旋转字符串(字符串):对于一个给定的字符序列S,请你把其循环左移K位后的序列输出。

    1 题目描述 汇编语言中有一种移位指令叫做循环左移(ROL),现在有个简单的任务,就是用字符串模拟这个指令的运算结果.对于一个给定的字符序列S,请你把其循环左移K位后的序列输出.例如,字符序列S=”a ...