﻿ Python二进制文件读取并转换为浮点数详解 - 军军小站|张军博客

# Python二进制文件读取并转换为浮点数详解

Python 3.6.5 |Anaconda custom (64-bit)|

python中一个float类型的数占4个字节。

```
import numpy as np
import struct
Python
```

```
# 加载测试数据
f = open('filename','rb')
# 102500为文档中包含的数字个数，而一个浮点数占4个字节
f.close()
verify_data = np.asarray(verify_data_raw).reshape(-1,1025)
```

```
split -b 820000k -a 2 filename data_
```

```
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']
```

```
for i in voc:
data_name = 'data_a'+str(i)
f = open(data_name,'rb')
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')
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')
np.save(data_name+'.npy',data_raw) # 保存data_bw.npy文件
```

QQ号联系： 360901061

【本文对您有帮助就好】