社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

如何使用python代码从二进制文件中读取和提取值?

Manu B Jayan • 6 年前 • 2202 次点击  

我对python比较陌生。作为我天文学项目工作的一部分,我必须处理二进制文件(当然这对我来说还是新的)。我得到了一个二进制文件和一个从二进制文件读取数据的python代码。然后我的教授让我理解代码是如何在二进制文件上工作的。我花了几天的时间想弄清楚,但没有任何帮助。这里有人能帮我查一下密码吗?

# Read the binary opacity file
f = open(file, "r")

# read file dimension sizes
a = np.fromfile(f, dtype=np.int32, count=16)
NX, NY, NZ = a[1], a[4], a[7]


# read the time and time step
time, time_step = np.fromfile(f, dtype=np.float64, count=2)

# number of iterations
nite = np.fromfile(f, dtype=np.int32, count=1)

# radius array
trash = np.fromfile(f, dtype=np.float64, count=1)
rad = np.fromfile(f, dtype=np.float64, count=a[1])

# phi array
trash = np.fromfile(f, dtype=np.float64, count=1)
phi = np.fromfile(f, dtype=np.float64, count=a[4])

# close the file
f.close()

据我所知,二进制文件包含几个参数(例如:半径、功率因数、声速、辐射能)及其许多值。上面的代码从二进制文件中提取值2参数radius和phi。半径和phi都有超过100个值。这个程序是有效的,但我不能理解它是如何工作的。任何帮助都将不胜感激。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/51609
文章 [ 2 ]  |  最新文章 6 年前
rgbarishian
Reply   •   1 楼
rgbarishian    7 年前
from astropy.io import ascii    
data = ascii.read('/directory/filename')
column1data = data[nameofcolumn1]
column2data = data[nameofcolumn2]

发射型计算机断层扫描仪。 column1data现在是该头下所有值的数组 我使用此方法导入ASCII格式的SourceExtractor dat文件。 我相信这是从ascii文件导入数据的更优雅的方法。

Asmus
Reply   •   2 楼
Asmus    7 年前

二进制文件实际上只是一长串连续数据;您需要告诉np.fromfile()两者 去哪里找 和 什么类型的数据 期待。 如果您创建自己的文件,可能最容易理解:

import numpy as np

with open('numpy_testfile', 'w+') as f:
    ## we create a "header" line, which collects the lengths of all relevant arrays
    ## you can then use this header line to tell np.fromfile() *how long* the arrays are
    dimensions=np.array([0,10,0,0,10,0,3,10],dtype=np.int32)
    dimensions.tofile(f) ## write to file

    a=np.arange(0,10,1) ## some fake data, length 10
    a.tofile(f) ## write to file
    print(a.dtype)

    b=np.arange(30,40,1) ## more fake data, length 10
    b.tofile(f) ## write to file
    print(b.dtype)

    ##  more interesting data, this time it's of type float, length 3
    c=np.array([3.14,4.22,55.0],dtype=np.float64) 
    c.tofile(f) ## write to file
    print(c.dtype)

    a.tofile(f) ## just for fun, let's write "a" again

with open('numpy_testfile', 'r+b') as f:
    ### what's important to know about this step is that 
    #   numpy is "seeking" the file automatically, i.e. it is considering 
    #   the first count=8, than the next count=10, and so on 
    #   as "continuous data"
    dim=np.fromfile(f,dtype=np.int32,count=8)
    print(dim) ## our header line: [ 0 10  0  0 10  0  3 10]
    a=np.fromfile(f,dtype=np.int64,count=dim[1])## read the dim[1]=10 numbers
    b=np.fromfile(f,dtype=np.int64,count=dim[4])## and the next 10
    ## now it's dim[6]=3, and the dtype is float 10
    c=np.fromfile(f,dtype=np.float64,count=dim[6] )#count=30)
    ## read "the rest", unspecified length, let's hope it's all int64 actually!
    d=np.fromfile(f,dtype=np.int64) 

print(a)
print(b)
print(c)
print(d)

附录: 这个 numpy documentation 当涉及到 阻止使用 属于 np.tofile() 和 np.fromfile() :

不要依赖tofile和fromfile的组合来存储数据,因为生成的二进制文件不是独立于平台的。特别是,不保存字节顺序或数据类型信息。数据可以以独立于平台的.npy格式存储,而不是使用save和load。

个人旁注: 如果你花了几天时间来理解这段代码,不要对学习感到气馁 python ;我们都从某个地方开始。我建议你诚实面对你给教授带来的障碍(如果这是在谈话中提到的),因为她/他在编程时应该能够正确地断言“你在哪里”。:-)