Skip to content
Advertisement

Reading next line of big text file

I’m new to python. I have a huge text file (about 15gb), so it is not possible to read in all the lines, I have read here how to fix that problem. Now about my problem. Is it possible to read the next line within a for loop without jumping to the beginning of the loop.

This is what I mean

JavaScript

When my text looks like this.

JavaScript

I want the output to be like this

JavaScript

Advertisement

Answer

You can do this using an iter() and next().

JavaScript

Given the input file:

JavaScript

it outputs:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement