0

I am having difficulty parsing a line from an hdr file I have. When I print read (data) like in the code below the command window outputs the contents of the hdr file. However, when I try to parse out a line or a column , like the script below, it outputs nothing in the command window.

import numpy as np
import matplotlib.pyplot as plt

f = open('zz_ssmv11034tS__T0001TTNATS2012021505HP001.Hdr', 'r')
data = f.read()
print (data)

for line in f:
    columns = line.split()
    time = float(columns[2])
    print (time)

f.close()
strak5587
  • 17
  • 3

1 Answers1

0

Remove this two lines and execute your code again:

data = f.read()
print (data)

Then change your loop:

for line in f.readlines():
    columns = line.split()
    time = float(columns[2])
    print (time)

Calling read() reads through the entire file and leaves the read cursor at the end of the file (with nothing more to read). If you are looking to read a certain number of lines at a time you could use readline(), readlines()

Read the post Why can't I call read() twice on an open file?

Community
  • 1
  • 1
RaminNietzsche
  • 2,683
  • 1
  • 20
  • 34
  • Removing the line `print (data)` will have no effect; the cursor is already at the end of the file at that point. – DSM Mar 29 '17 at 15:53
  • He said " When I print read (data) like in the code below the command window outputs the contents of the hdr file" and after that cursor pointed to end of file, I mean to remove this line and try again – RaminNietzsche Mar 29 '17 at 15:56
  • 1
    Your answer begins "Remove this line: `print (data)`". Doing that will change absolutely nothing. – DSM Mar 29 '17 at 15:58
  • @DSM Sorry, That's my fault, I changed it. – RaminNietzsche Mar 29 '17 at 16:00