[Tutor] Finding line number by offset value.

Steven D'Aprano steve at pearwood.info
Sun Feb 21 21:24:54 EST 2016


On Mon, Feb 22, 2016 at 01:41:42AM +0000, Alan Gauld wrote:
> On 21/02/16 19:32, Cody West wrote:

> > I'm trying to take 48L, which I believe is the character number, and get
> > the line number from that.
> 
> I'm not totally clear what you mean but, if it is that 48L
> is the character count from the start of the file and you
> want to know the line number then you need to count the
> number of \n characters between the first and 48th
> characters.
> 
> But thats depending on your line-end system of course,
> there may be two characters on each EOL... 

Provided your version of Python is built with "universal newline 
support", and nearly every Python is, then if you open the file in text 
mode, all end-of-lines are automatically converted to \n on reading.

If the file is small enough to read all at once, you can do this:

offset = 48
text = the_file.read(offset)
print text.count('\n')


to print a line number starting from 0.

If the file is too big to read all at once, you can do this:

# untested
running_total = 0
line_num = -1
offset = 48000000  # say
for line in the_file:
    running_total += len(line)
    line_num += 1
    if running_total >= offset:
        print line_num
        break
    

-- 
Steve


More information about the Tutor mailing list