我有一个二进制文件,它是* .xyz,一个二进制plot3d格式文件。我不知道如何从python读取它。打开txt副本没有问题,但是如何直接打开该二进制文件?
二进制文件的样子(前30行):
0400 0000 0a00 0000 0400 0000 7800 0000
0200 0000 2100 0000 0100 0000 0200 0000
2100 0000 0100 0000 0200 0000 0100 0000
1500 0000 0200 0000 0100 0000 1500 0000
0200 0000 0100 0000 1900 0000 0200 0000
0100 0000 1900 0000 0200 0000 0100 0000
1500 0000 0200 0000 0100 0000 1500 0000
0200 0000 0100 0000 1500 0000 0200 0000
0100 0000 1500 0000 7800 0000 3807 0000
0000 0060 3bdf ef3f 0000 0060 3bdf ef3f
ac1c 5a64 3bdf ef3f ac1c 5a64 3bdf ef3f
ac1c 5a64 3bdf ef3f ac1c 5a64 3bdf ef3f
ac1c 5a64 3bdf ef3f ac1c 5a64 3bdf ef3f
ac1c 5a64 3bdf ef3f ac1c 5a64 3bdf ef3f
ac1c 5a64 3bdf ef3f ac1c 5a64 3bdf ef3f
ac1c 5a64 3bdf ef3f ac1c 5a64 3bdf ef3f
ac1c 5a64 3bdf ef3f ac1c 5a64 3bdf ef3f
ac1c 5a64 3bdf ef3f ac1c 5a64 3bdf ef3f
ac1c 5a64 3bdf ef3f ac1c 5a64 3bdf ef3f
ac1c 5a64 3bdf ef3f ac1c 5a64 3bdf ef3f
ac1c 5a64 3bdf ef3f ac1c 5a64 3bdf ef3f
ac1c 5a64 3bdf ef3f ac1c 5a64 3bdf ef3f
ac1c 5a64 3bdf ef3f ac1c 5a64 3bdf ef3f
ac1c 5a64 3bdf ef3f ac1c 5a64 3bdf ef3f
ac1c 5a64 3bdf ef3f ac1c 5a64 3bdf ef3f
ac1c 5a64 3bdf ef3f ac1c 5a64 3bdf ef3f
ac1c 5a64 3bdf ef3f ac1c 5a64 3bdf ef3f
ac1c 5a64 3bdf ef3f ac1c 5a64 3bdf ef3f
ac1c 5a64 3bdf ef3f ac1c 5a64 3bdf ef3f
ac1c 5a64 3bdf ef3f ac1c 5a64 3bdf ef3f
对应的文本如下(前30行):
10
2 33 1 2 33 1
2 1 21 2 1 21
2 1 25 2 1 25
2 1 21 2 1 21
2 1 21 2 1 21
0.995999991893768 0.995999991893768 0.996000000000000
0.996000000000000 0.996000000000000 0.996000000000000
0.996000000000000 0.996000000000000 0.996000000000000
0.996000000000000 0.996000000000000 0.996000000000000
0.996000000000000 0.996000000000000 0.996000000000000
0.996000000000000 0.996000000000000 0.996000000000000
0.996000000000000 0.996000000000000 0.996000000000000
0.996000000000000 0.996000000000000 0.996000000000000
0.996000000000000 0.996000000000000 0.996000000000000
0.996000000000000 0.996000000000000 0.996000000000000
0.996000000000000 0.996000000000000 0.996000000000000
0.996000000000000 0.996000000000000 0.996000000000000
0.996000000000000 0.996000000000000 0.996000000000000
0.996000000000000 0.996000000000000 0.996000000000000
0.996000000000000 0.996000000000000 0.996000000000000
0.996000000000000 0.996000000000000 0.996000000000000
0.996000000000000 0.996000000000000 0.996000000000000
0.996000000000000 0.996000000000000 0.996000000000000
0.996000000000000 0.996000000000000 0.996000000000000
0.996000000000000 0.996000000000000 0.996000000000000
0.996000000000000 0.996000000000000 0.996000000000000
0.996000000000000 0.995999991893768 0.995999991893768
2.333463643677469E-003 2.333463643677469E-003 2.219304467294528E-003
2.219304467294528E-003 2.105145290300329E-003 2.105145290300329E-003
对此,我在这里有几个问题:
1:是这种情况,首先我需要将二进制文件转换为文本信息,然后再像读取文本文件一样读取它吗?
2:是这种情况,例如python之类的编程语言能够在不事先知道格式的情况下将二进制信息转换为文本信息吗?
with open('oldsurf.p3d', 'rb') as OldSurf:
bn=next(OldSurf).split()
BlockNumber=int(bn[0])
print('There are ', BlockNumber, ' Blocks in total.')
BlockDimension=[[0 for i in range(3)] for bi in range(BlockNumber)]
ln=(BlockNumber-1)//2+1
for li in range(ln):
d=next(OldSurf).split()
bi=li*2
for i in range(3):
BlockDimension[bi][i]=int(d[i])
if len(d)==6:
bi=li*2+1
for i in range(3):
BlockDimension[bi][i]=int(d[i+3])
print('The Dimensions of Blocks are: ', BlockDimension)
value=[]
for line in OldSurf:
for word in line.split():
value.append(float(word))
这个问题已经存在很长时间了,仍然没有找到可行的解决方案,欢迎任何可行的解决方案,谢谢!!
答案 0 :(得分:0)
我实际上知道如何在Fortran中阅读它。我只是不知道python是如何工作的。也许我应该问的是:任何人都可以帮助将以下Fortran代码解释为python:
parameter ( imax = 100 )
parameter ( jmax = 100 )
parameter ( kmax = 100 )
parameter ( nbmax = 10 )
integer i
integer j
integer m
integer n
integer nblocks
integer ni (nbmax)
integer nj (nbmax)
integer nk (nbmax)
real mach ! freestream Mach number
real alpha ! freestream angle-of-attack
real reyn ! freestream Reynolds number
real time ! time
real x(imax,jmax,kmax,nbmax)
real y(imax,jmax,kmax,nbmax)
real z(imax,jmax,kmax,nbmax)
real q(imax,jmax,kmax,nbmax,5)
open ( unit=7, form='unformatted', file='3D.x' )
open ( unit=8, form='unformatted', file='3D.q' )
read(7) nblocks
read(7) ( ni(m), nj(m), nk(m), m = 1, nblocks )
do m = 1, nblocks
read(7)
& ((( x(i,j,k,m), i=1,ni(m)), j=1,nj(m)), k=1,nk(m)),
& ((( y(i,j,k,m), i=1,ni(m)), j=1,nj(m)), k=1,nk(m)),
& ((( z(i,j,k,m), i=1,ni(m)), j=1,nj(m)), k=1,nk(m))
enddo
read(8) nblocks
read(8) ( ni(m), nj(m), nk(m), m = 1, nblocks )
do m = 1, nblocks
read(8) mach, alpha, reyn, time
read(8)
& (((( q(i,j,k,m,n), i=1,ni(m)), j=1,nj(m)), k=1,nk(m)), n=1,5)
enddo
答案 1 :(得分:0)
好的,我使用FortranFile模块自己解决了这个问题:
from scipy.io import FortranFile
有:
read_ints()
read_reals()
详细信息: https://docs.scipy.org/doc/scipy/reference/generated/scipy.io.FortranFile.html