Question

I want to analyze a specific part of a txt.file (lines 48-392) and I have problems numbering the single lines of the txt file. This is what I was able to come up so far:

with open ('kafka_hungerkuenstler.txt', encoding = 'utf-8') as f:
    for line in f:
    #I tried: enumerate (line)
    #there should also be something like read_selected_lines (filename, 48,392) somewhere in
     the code      
        print (line) 
Was it helpful?

Solution

Use itertools.islice() to pick a certain range of lines:

from itertools import islice

with open ('kafka_hungerkuenstler.txt', encoding = 'utf-8') as f:
    for line in islice(f, 47, 392):

The loop will only iterate over lines 47 (counting from zero) until 391, inclusive; counting from 1 that's line 48 through to 392, inclusive.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top