阅读与Python FORTRAN格式化阵列没有numpy的 [英] Reading fortran unformatted arrays with python without numpy

查看:203
本文介绍了阅读与Python FORTRAN格式化阵列没有numpy的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很新到Python。我试图通过使用Python来读取一个未格式化的Fortran文件。不过,我想构造读入数组的元素。 I $ ppared给你一个样本,以帮助你理解我的问题P $。在这里,下面你可以找到一个Fortran作家写这三个整数,然后用三双precision元素的线。

I am quite new to Python. I am trying to read an unformatted Fortran file by using Python. However, I would like to structure the elements read into arrays. I prepared for you a sample to help you understand my problem. Here below you can find a Fortran writer which writes three integers and then a line with three double precision elements.

    program writer
    integer :: dummy,dummy2,dummy3
    double precision,dimension(3) :: dummy_double
          dummy=1
          dummy2=2
          dummy3=3
          dummy_double(1)=4.23e0
          dummy_double(2)=5.4e0
          dummy_double(3)=7.61e0

          open(100,file="binary",form="unformatted",convert="little_endian")
          write(100) dummy
          write(100) dummy2
          write(100) dummy3
          write(100) dummy_double(1:3)
          close(100)
          end

我现在想读与Python这个文件,而无需使用numpy的。我知道每个记录的结束和开始INTEGER * 4。我在文件开头跳过前4位,那么,我读的第一个整数,然后我跳过8位(+年底开始)等。无分离器是写在一个相同的序列,这是双precision不是由INTEGER * 4分离(纠正我,如果我错了..)。因为我无法读取的最后一行,用3双precision记录我写信给你。我想将它们存储在补偿[3] 阵列。

import struct
with open("binary", "rb") as f:
#    while True:
        dummy = f.read(4)
        print "dummy=",struct.unpack('i', f.read(4))[0]
        dummy = f.read(8)
        print "dummy2=",struct.unpack('i', f.read(4))[0]
        dummy = f.read(8)
        print "dummy3=",struct.unpack('i', f.read(4))[0]
        comp = [0] * 3
        print "length=",len(comp)
        dummy = f.read(8)
        comp=struct.unpack('<d', f.read(8))[0:2]
        print "comp=",comp[0],comp[1],comp[2]

我正的错误是:

$carlo: python Script_Library.py
dummy= 1
dummy2= 2
dummy3= 3
length= 3
comp= 4.23000001907
Traceback (most recent call last):
  File "Script_Library.py", line 14, in <module>
    print "comp=",comp[0],comp[1],comp[2]
IndexError: tuple index out of range

整数被正确读取,以及所述第一双precision元素。然而,它们不是正确地存储。请你纠正脚本的这些不工作的部分?

Integers are correctly read as well as the first double precision elements. However, they are not correctly stored. Would you please correct the parts of the script which are not working?

推荐答案

问题就解决了​​感谢的 durasm

the problem is solved thanks to durasm.

我在这里报告的工作codeS:

I report here the working codes:

program writer
integer :: dummy,dummy2,dummy3
double precision,dimension(3) :: dummy_double
          dummy=1
          dummy2=2
          dummy3=3
          dummy_double(1)=4.23e0
          dummy_double(2)=5.4e0
          dummy_double(3)=7.61e0

          open(100,file="binary",form="unformatted",convert="little_endian")
          write(100) dummy
          write(100) dummy2
          write(100) dummy3
          write(100) dummy_double(1:3)
          close(100)
end

下面下面你可以找到纠正脚本。首先,没有必要分配补偿。然后,我们有三双precision记录读取,相当于 24位和三次 DDD

Here below you can find the corrected script. First of all, no need to allocate comp. Then, we have three double precision records to read, equivalent to 24 bits and three times ddd.

import struct
with open("binary", "rb") as f:
        dummy = f.read(4)
        print "dummy=",struct.unpack('i', f.read(4))[0]
        dummy = f.read(8)
        print "dummy2=",struct.unpack('i', f.read(4))[0]
        dummy = f.read(8)
        print "dummy3=",struct.unpack('i', f.read(4))[0]
        dummy = f.read(8)
        comp=struct.unpack('<ddd', f.read(24))
        print "comp=",comp[0],comp[1],comp[2]

和结果:

$carlo: python Script_Library.py
dummy= 1
dummy2= 2
dummy3= 3
comp= 4.23000001907 5.40000009537 7.61000013351

这篇关于阅读与Python FORTRAN格式化阵列没有numpy的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆