Python二进制文件读取并转换为浮点数详解
作者:淡淡博客 时间:2022-12-16 19:10:40
本文所用环境:
Python 3.6.5 |Anaconda custom (64-bit)|
引言
由于某些原因,需要用python读取二进制文件,这里主要用到struct包,而这个包里面的方法主要是unpack、pack、calcsize。详细介绍可以看:Python Struct 官方文档。这里主要讨论,python二进制转浮点数的操作。
python中一个float类型的数占4个字节。
二进制数据转float,可以用struct.unpack()来实现。
小文件读取
较小的文件,可以一次读取:
首先导入所需的包:
import numpy as np
import struct
Python
例如:我需要读取一个名为filename,存放着形状为[100,1025]的浮点数的文件。可以采用以下办法
# 加载测试数据
f = open('filename','rb')
# 102500为文档中包含的数字个数,而一个浮点数占4个字节
data_raw = struct.unpack('f'*102500,f.read(4*102500))
f.close()
verify_data = np.asarray(verify_data_raw).reshape(-1,1025)
大文件处理方法
我需要处理的文件大小有38.1G,存放着[10000000,1025]大小的向量。
关于大文件的处理,我参考了这文章,但是,这个方法不能很好的将二进制文件转换成浮点数。
所以我想到了另外一种办法:
通过Linux命令切割文件
通过split命令将38.1G的文件按照指定大小切割,
split -b 820000k -a 2 filename data_
上述代码的意思是,指定每块大小为820000k,-a 2代表2位数命名,‘data_'代表前缀是'data_'
最终生成49个文件(字典序 aa – bw),前48个文件每个204800行 最后一个文件 169600行
通过python循环读取文件
首先构建词汇表:
voc = ['a','b','c','d','e','f','g','h','i','j','k','l',
'm','n','o','p','q','r','s','t','u','v','w','x',
'y','z']
voc_short = ['a','b','c','d','e','f','g','h','i','j','k','l',
'm','n','o','p','q','r','s','t','u','v']
为了方便读取,将49个二进制文件转换成numpy专用二进制格式*.npy
for i in voc:
data_name = 'data_a'+str(i)
f = open(data_name,'rb')
data_raw = struct.unpack('f'*209920000,f.read(4*209920000))
f.close()
data = np.asarray(data_raw).reshape(-1,1025)
np.save(data_name+'.npy',data) # 保存data_a*.npy文件
for i in voc_short:
data_name = 'data_b'+str(i)
f = open(data_name,'rb')
data_raw = struct.unpack('f'*209920000,f.read(4*209920000))
f.close()
data = np.asarray(data_raw).reshape(-1,1025)
np.save(data_name+'.npy',data) # 保存data_b*.npy文件
data_name = 'data_bw'
f = open(data_name,'rb')
data_raw = struct.unpack('f'*173840000,f.read(4*173840000))
np.save(data_name+'.npy',data_raw) # 保存data_bw.npy文件
来源:https://blog.i-ll.cc/archives/447
标签:python,二进制,转换,浮点数
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python ini文件常用操作方法解析
2022-01-07 09:52:44
python之import机制详解
2022-11-29 02:47:40
![](https://img.aspxhome.com/file/2023/7/118767_0s.jpg)
使用golang如何优雅的关机或重启操作示例
2024-05-09 09:55:56
block 和 inline 的区别是?
2009-12-08 13:00:00
![](https://img.aspxhome.com/file/UploadPic/200912/8/203330321-54s.gif)
python 多线程应用介绍
2023-07-13 08:04:04
js中bool值的转换及“&&”、“||”、 “!!”详解
2024-04-19 10:02:01
网页版面布局的处理问题
2008-06-05 12:32:00
golang中json操作的完全指南
2024-04-23 09:35:00
![](https://img.aspxhome.com/file/2023/1/134411_0s.png)
浅析Oracle和Mysql分页的区别
2024-01-26 19:03:59
Django学习之静态文件与模板详解
2022-12-13 13:19:58
![](https://img.aspxhome.com/file/2023/5/107875_0s.png)
python使用js2py库运行js代码
2022-09-06 17:06:55
python学生信息管理系统(完整版)
2023-06-25 05:16:31
Python二维列表的创建、转换以及访问详解
2022-08-09 16:03:15
python 美化输出信息的实例
2022-04-15 09:53:54
![](https://img.aspxhome.com/file/2023/7/79757_0s.jpg)
Burpsuite模块之Burpsuite Intruder模块详解
2023-11-24 05:31:24
![](https://img.aspxhome.com/file/2023/1/99391_0s.jpg)
SQLSERVER记录登录用户的登录时间(自写脚本)
2024-01-27 15:07:24
![](https://img.aspxhome.com/file/2023/7/129867_0s.jpg)
vue 代码压缩优化方式
2024-04-09 10:44:46
Python+django实现文件上传
2022-08-31 20:52:22
python 三边测量定位的实现代码
2023-02-03 08:37:31
![](https://img.aspxhome.com/file/2023/2/87912_0s.jpg)
如何让12px以下的文字最佳显示
2008-01-24 18:39:00