0

This is my code:

    f = gzip.open('nome_file.gz','r')

    line = f.readline()

    for line in f:
            line = f.readline()
            line = line.strip('\n')
            if not line: break
            elements = line.split(" ")
            print elements[0]," ",elements[1]," ",elements[44]," ",elements[45]

    f.close()

I really don't know why just one line over two is read.

tshepang
  • 12,111
  • 21
  • 91
  • 136
user2961420
  • 267
  • 1
  • 3
  • 8

1 Answers1

4

for line in f: reads a line. The next line line = f.readline() reads the next line and stores it in the same variable.

You read every line, but skip every second one.

Simply dropping line = f.readline() should solve the problem.

mirk
  • 5,302
  • 3
  • 32
  • 49
  • 1
    Moreover, in the default file object implementation, `.readline()` and `next(f)` use *different* buffers and you cannot mix the two. – Martijn Pieters Nov 14 '13 at 10:22
  • "-.- Thank you! I have also another problem, how can I skip the first line? Because in this way (f.readline(0)) it doesn't work. – user2961420 Nov 14 '13 at 10:22
  • You can flag the question as solved, by clicking on the "V" below the answer-score. – mirk Nov 25 '13 at 11:52
  • @MartijnPieters can you explain a little more; I don't understand what you mean by *you cannot mix the two*. – tshepang Sep 08 '14 at 06:02