Reading unformatted big-endian files

Andrea Gavana andrea.gavana at gmail.com
Fri Aug 11 05:19:40 EDT 2006


Hello NG,

    that may sound a silly question, but I didn't find anything really
clear about the issue of reading unformatted big endian files with
Python. What I was doing till now, was using Fortran to read those
files and compile this Fortran extension using F2PY. Now that it seems
that no possible combinations of Fortran/C compilers actually *work*
with Python 2.4 on Windows XP, I was trying to translate the Fortran
subroutine to Python. Basically, what I do (in Fortran, I hope to
explain the code clearly) is:

! Declare an integer
integer number

! Declare a 4-chars character
character*4 keytype

! Declare a 8-chars character
character*8 keyword

! feof is not very important here
logical feof
feof = .false.

! Open the file as unformatted big-endian
open(unit = , file = filename, form = 'UNFORMATTED', convert = 'BIG_ENDIAN')

! loop until you find a particular keyword
! here "end=10" means that if the routine finds the EOF, it should go to
! the label "10 continue". "err=8" means that, if an error occours in
reading the file,
! it should go to the label "8 continue" and continue reading the file

do while(.not.feof)

    ! Read the 3 variables keyword, number and keytype
    read(1, end=10, err=8) keyword, number, keytype

    ! If the keyword is 'DIMENS', break the loop and go to the end
    if (keyword == 'DIMENS') then
        read(1, end=10, err=8) dimens
        goto 10
    endif
8  continue
enddo
	
10    continue

! Close the file
close(1)


Well, does anyone have some suggestion about which kind of
material/tutorial on similar things I should read? How can I deal in
Python with variables that must be 8-chars or 4-chars in order to read
correctly the file? Am I missing something else?

Thank you very much for every suggestion.

Andrea.

-- 
"Imagination Is The Only Weapon In The War Against Reality."

http://xoomer.virgilio.it/infinity77/



More information about the Python-list mailing list