Python的Struct模块
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模块的更多相关文章
- Python学习——struct模块的pack、unpack示例
he struct module includes functions for converting between strings of bytes and native Python data t ...
- c语言write与python的struct模块交互
以下讲的都是用二进制形式打开文件.网上有很多struct模块的文章,下面是我做的小实验. 1.对于c里面的fwrite写入一个单字节,写的就是它的二进制.如3,写入文件就是二进制0x03,它并不是3的 ...
- python中struct模块及packet和unpacket
转自:http://www.cnblogs.com/gala/archive/2011/09/22/2184801.html 我们知道python只定义了6种数据类型,字符串,整数,浮点数,列表,元组 ...
- 【转】在Python的struct模块中进行数据格式转换的方法
这篇文章主要介绍了在Python的struct模块中进行数据格式转换的方法,文中还给出了C语言和Python语言的数据类型比较,需要的朋友可以参考下 Python是一门非常简洁的语言,对于数据类型的表 ...
- day30 python学习 struct模块和 subprocess 模块
import subprocess import struct aa=input('>>') obj=subprocess.Popen(aa,shell=True,#aa代表的是读取字符串 ...
- Python:struct模块的pack、unpack
mport struct pack.unpack.pack_into.unpack_from 1 # ref: http://blog.csdn<a href="http://lib. ...
- python中struct模块
# #********struct模块********# # 1.按照指定格式将Python数据转换为字符串,该字符串为字节流,如网络传输时, # 不能传输int,此时先将int转化为字节流,然后再发 ...
- Python之struct模块
面对网络协议,在组包拆包时,python提供了struct模块,它可以帮助我们在python值和C语言的结构体之间相互转换,下面一起来了解struct的具体用法. 假设,我们的网络协议为消息id(un ...
- Python struct模块
有的时候需要用python处理二进制数据,比如,存取文件,socket操作时.这时候,可以使用python的struct模块来完成.可以用 struct来处理c语言中的结构体. struct模块中最重 ...
随机推荐
- 启动elasticsearch的时候报出Exception in thread "main" SettingsException[Failed to load settings from /usr/local/elasticsearch/config/elasticsearch.yml]; nested: MarkedYAMLException[while scanning a simple ke
故障现象: [elasticsearch@tiantianml- ~]$ /usr/local/elasticsearch/bin/elasticsearch Exception in thread ...
- 前端如何避免bug的产生?
项目环境:react生态圈 界面功能基本和:增(新增一条数据).删(删除一条数据).查(展示列表).改(修改数据)挂钩. 一.展示数据列表相关[判空,控制显示距离,分页是否有效,搜索是否有效] 1.渲 ...
- 关于react中遇到的问题记录说明
5.el表达式 dataSource = (userPage, orgList) => userPage.map(item => { const org = orgList.find(or ...
- XGBoost原理详解
原文:https://blog.csdn.net/qq_22238533/article/details/79477547
- 用于Python文件转换.exe文件的pyinstaller工具安装
安装方法: 注:python环境一定要配置好. 1.第一步:下载 官方网站:http://www.pyinstaller.org/downloads.html 此处下载版本为稳定版. 2.第二步:下载 ...
- centos7安装过程中的安装源设置
centos7的安装源设置:http://mirrors.aliyun.com/centos/7/os/x86_64/
- Vue的作用域插槽
一.通常情况下都是父组件传递数据给子组件进行展示的(无法改变子组件的展示方式):而作用域插槽允许子组件通过slot向父组件传递数据,类似React中的“以函数为子组件”,由父组件决定渲染的内容(包含绑 ...
- Rocketmq-简单部署
一.准备环境 1.系统:Centos7.3(无硬性要求) 2. jdk:1.8 3.maven:3.5(无硬性要求) 4.git 5.rocketmq 4.2 二.环境部署 1.jdk1.8以及mav ...
- c# 所有类型都是从object继承,那么值类型默认也有装箱吗?
我们知道,c#所有类型都是从System.Object继承,int等值类型也逃脱不了这种命运,那难道值类型默认有装箱操作吗?答案是否,在CLR via这本书中有简短的解释说明: 1.值类型从Syste ...
- go 计算 MD5
Golang的加密库都放在crypto目录下,其中MD5库在crypto/md5包中,该包主要提供了New和Sum函数 直接调用md5计算 package main import ( "cr ...