user2961420
user2961420

Reputation: 277

Reading compressed file line by line

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.

Upvotes: 0

Views: 215

Answers (1)

mirk
mirk

Reputation: 5520

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.

Upvotes: 4

Related Questions