On Monday, October 31, 2016 at 8:03:53 PM UTC+1, MRAB wrote: > On 2016-10-31 17:46, Heli wrote: > > On Monday, October 31, 2016 at 6:30:12 PM UTC+1, Irmen de Jong wrote: > >> 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 > > > > Thanks Irmen, > > > > I tried with "rU" but that did not make a difference. The problem is a line > > that with one single write statement in my fortran code : > > > > write(UNIT=9,FMT="(99g20.8)") value > > > > seems to be read in two python inputfile.readline(). > > > > Any ideas how I should be fixing this? > > > > Thanks, > > > What is actually in the file? > > Try opening it in binary mode and print using the ascii function: > > with open("fortranfile.txt", "rb") as f: > contents = f.read() > > print("CONTENTS:", ascii(contents))
Thanks guys, I solved the problem on the Fortran side. Some lines contained new lines characters that I fixed by setting format descriptors in Fortran. Thanks for your help, -- https://mail.python.org/mailman/listinfo/python-list