Python读取二进制文件代码方法解析
作者:kurrrr 发布时间:2023-06-14 14:32:35
标签:Python,读取,二进制,文件
问题
有二进制文件中保存了 20 亿个 2 Bytes 的数,需将其读出,每 20000 个数作图,拟合后输出结果。
解决
# -*- coding: utf-8 -*-
"""
@author: kurrrr
"""
import struct
def main():
data_file = open('run0035.bin', 'rb')
data_temp = data_file.read(2)
data_short, = struct.unpack('h', data_temp)
print(data_short)
if __name__ == '__main__':
main()
总结
open 时加上 b 关键词
read() 函数实现读取,参数为读取的字节数
使用 struct 模块中的 unpack() 函数将二进制转化为十进制,注意 unpack() 函数返回的是 tuple,因此需要用 data_short, = struct.unpack(‘h', data_temp)
关于 struct 模块中的 format 具体可在官网上找到。
来源:https://www.cnblogs.com/kurrrr/p/13160678.html
0
投稿
猜你喜欢
- 看代码吧~import numpy as npa = np.array([[2, 4, 6, 1], [1, 5, 2, 9]])print
- 本文细致的为大家分享了Bootstrap轮播插件简单实例,供大家参考,具体内容如下使用Bootstrap的轮播插件可以向站点添加滑块,内容可
- 1.指向“开始->程序->Microsoft SQL Server 2005->配置工具->SQL Server 外
- 1.直方图直方图: (1) 图像中不同像素等级出现的次数 (2) 图像中具有不同等级的像素关于总像素数目的比值。我们使用cv2.calcHi
- 首先创建一个表Use Test;Create Table TableTest(`ID` mediumint(8) default '
- 大家都知道在python中,一切皆对象,变量也不再具有类型,变量仅仅是对象的一个引用,我们通常用变量来测类型,通常测得就是被这个变量引用得对
- 基本映射映射使用在根据不同URLs请求来产生相对应的返回内容.Bottle使用route() 修饰器来实现映射.from bottle im
- 本文实例讲述了PHP+Ajax简单get验证操作。分享给大家供大家参考,具体如下:1、显示页面代码index.html<!DOCTYP
- 左为旧版,右为更新到1.0版本后的名字定义变量的更新tf.VARIABLES ——> tf.GLOBAL_VARIABLEStf.al
- SQL*DBA命令的安全性: 如果您没有SQL*PLUS应用程序,您也可以使用SQL*DBA作SQL查权限相关的命令只能分配给Oracle软
- “In the latest release 10.2 Oracle changed these default values. The m
- 使用OpenCV's Haar cascades作为人脸检测,因为他做好了库,我们只管使用。代码简单,除去注释,总共有效代码只有10
- 今天业务上碰到用pandas处理一个大文件的内存不够问题,需要做concat 合并多个文件,每个文件数据在1.4亿行左右。当时第一反应是把d
- 1、从MYSQL官方下载MYSQL的源码版本【一定要是源码版本】 2、按以下代码键入LINUX命令行 [注] 添加mysql组和用户 #gr
- 所谓的模块导入( import ),是指在一个模块中使用另一个模块的代码的操作,它有利于代码的复用。在 Python 中使用 import
- 一、什么是协程协程拥有自己的寄存器和栈。协程调度切换的时候,将寄存器上下文和栈都保存到其他地方,在切换回来的时候,恢复到先前保存的寄存器上下
- Matplotlib 是一个 Python 的 2D绘图库,它以各种硬拷贝格式和跨平台的交互式环境生成出版质量级别的图形
- 在SQL Server 中,如果给表的一个字段设置了默认值,就会在系统表sysobjects中生成一个默认约束。如果想删除这个设置了默认值的
- getpwname只能得到gid一个username。import pwdmyGroupId = pwd.getpwnam(username
- 学习python字符串方法的使用,对书中列举的每种方法都做一个试用,将结果记录,方便以后查询。(1) s.capitalize() ;功能: