How can I get python to read every nth line of a .txt file?

27,371

Solution 1

You can't... [do it using pure I/O functions] You have to read all lines and simply make your code ignore the lines that you do not need.

For example:

with open(filename) as f:
    lines = f.readlines()
desired_lines = lines[start:end:step]

In the code above replace start, end, and step with desired values, e.g., "...if I wanted to read line 2 of a .txt file, but every 4 lines after that..." you would do like this:

desired_lines = lines[1::4]

Solution 2

A little late to the party, but here is a solution that doesn't require to read the whole file contents into RAM at once, which might cause trouble when working with large enough files:

# Print every second line.
step = 2
with open("file.txt") as handle:
    for lineno, line in enumerate(handle):
        if lineno % step == 0:
            print(line)

File objects (handle) allow to iterate over lines. This means we can read the file line by line without accumulating all the lines if we don't need to. To select every n-th line, we use the modulo operator with the current line number and the desired step size.

Solution 3

You can first open the file as f with a with statement. Then, you can iterate through every line in the file using Python's slicing notation.

If we take f.read(), we get a string with a new-line (\n) character at the end of every line:

"line1\nline2\nline3\n"

so to convert this to a list of lines so that we can slice it to get every other line, we need to split it on each occurrence of \n:

f.read().split()

which for the above example will give:

["line1", "line2", "line3"]

Finally, we need to get every-other line, this is done with the slice [::2]. We know this from the way that slicing works:

list[start : stop : step]

Using all this, we can write a for-loop which will iterate through every-other line:

with open("file.txt", "r") as f:
    for line in f.read().split("\n")[::2]:
        print(line)
Share:
27,371
superasiantomtom95
Author by

superasiantomtom95

Updated on December 02, 2020

Comments

  • superasiantomtom95
    superasiantomtom95 over 3 years

    If I wanted python to read every 2nd (or every 4th) line of a file, how could I tell it to do so? Additionally, if I wanted to read line 2 of a .txt file, but every 4 lines after that (next line would be line 6, then 10, and so on forth), how could I make it do so?

  • Joe Iddon
    Joe Iddon over 6 years
    Python doesn't use arrays, it uses lists, and this just won't work...
  • Joe Iddon
    Joe Iddon over 6 years
    You also need a space between the end of the function definition and the call to the function...
  • Rehan Shikkalgar
    Rehan Shikkalgar over 6 years
    it is just name of variable, have you tried executing this code?
  • Joe Iddon
    Joe Iddon over 6 years
    Yes, it fails in many ways... And I know its just a variable name, but it just shows a lack of understanding, that's all.
  • AGN Gazer
    AGN Gazer over 6 years
    I do not know why your answer was down-voted but I can reassure you it was not me.
  • Joe Iddon
    Joe Iddon over 6 years
    @AGNGazer Don't worry, I wasn't accusing anyone, I just wanted to see what someone thought was not useful about it... A downvote without a comment isn't that helpful
  • Rehan Shikkalgar
    Rehan Shikkalgar over 6 years
    can you give me example where it fails?
  • Joe Iddon
    Joe Iddon over 6 years
    Well it will give an IndexError even if you leave a line before the call to the function. This is because you are adding all the lines in the file to a list and then iterating through every index in this list. Then, you try to get the value from the list of that index multiplied by the input. This means that unless the input is 1, you are going to be trying to get an element in the list which is way over the end - giving an IndexError. You can fix the error by making the second for-loop go to len(array)/lineNo. Nevertheless, atm your code does not work as I said.
  • Tomerikoo
    Tomerikoo over 3 years
    Probably the fact that this reads the whole file into a string instead of iterating the file line-by-line...
  • Tomerikoo
    Tomerikoo over 3 years
    Downvoted because of the statement: "You have to read all lines and simply make your code ignore the lines that you do not need". Not true and not necessary, see other answers here... This creates two unnecessary lists
  • Tomerikoo
    Tomerikoo over 3 years
    This will fail for the second example of start from line 2 and take every 4 lines. You would expect to take line 6, but 6 % 4 != 0...