Reading Fortran Ascii output using python

Irmen de Jong irmen.NOSPAM at xs4all.nl
Mon Oct 31 13:29:49 EDT 2016


On 31-10-2016 18:20, Heli wrote:
> Hi all, 
> 
> I am trying to read an ascii file written in Fortran90 using python. I am reading this file by opening the input file and then reading using:
> 
> inputfile.readline()
> 
> On each line of the ascii file I have a few numbers like this:
> 
> line 1: 1
> line 2: 1000.834739 2000.38473 3000.349798 
> line 3: 1000 2000 5000.69394 99934.374638 54646.9784
> 
> The problem is when I have more than 3 numbers on the same line such as line 3, python seems to read this using two reads. This makes the above example will be read like this:
> 
> line 1: 1
> line 2: 1000.834739 2000.38473 3000.349798 
> line 3: 1000 2000 5000.69394 
> line 4: 99934.374638 54646.9784
> 
> How can I fix this for each fortran line to be read correctly using python?
> 
> Thanks in Advance for your help, 
>  
> 

You don't show any code so it's hard to say what is going on.
My guess is that your file contains spurious newlines and/or CRLF combinations.

Try opening the file in universal newline mode and see what happens?

with open("fortranfile.txt", "rU") as f:
    for line in f:
        print("LINE:", line)


Irmen




More information about the Python-list mailing list