-1

i want to ask about tell() method. So, have code like this

op = open('data.txt', 'r')
pos = op.tell()
data = op.readline()
key = []
while data:
   pos = op.tell()
   data = op.readline()
   key.append(pos)

and the result

key[:3]
[[87], [152], [240]]

i want my key value start from 0 since it's the first pointer position for the beginning of the sentence. but it starts from the starting pointer value for 2nd sentence. sorry, i'm new to python.

the data looks like this. it's containing few rows

  Sanjeev Saxena#Parallel Integer Sorting and Simulation Amongst CRCW Models.
  Hans Ulrich Simon#Pattern Matching in Trees and Nets.
  Nathan Goodman#Oded Shmueli#NP-complete Problems Simplified on Tree Schemas.
Ai Chan
  • 33
  • 6

2 Answers2

1

You didn't add the first pointer to the key list (you have 2x pos = op.tell() before you do the first key.append(pos)).

You should just remove the 2nd and 3rd lines:

op = open('data.txt', 'r')
key = []
while data:
    pos = op.tell()
    data = op.readline()
    key.append(pos)
ruohola
  • 21,987
  • 6
  • 62
  • 97
  • I would change your code a bit and move `key.append(pos)` to the line right after finding it. – Zubda May 05 '19 at 13:47
  • @YHoffman Doesn't really make a difference, and this way it's more clear that only the 2 unnecessary lines were actually wrong in the original code. – ruohola May 05 '19 at 13:48
  • I agree and that is why I only commented (I upvoted your answer as I indeed think that you answered the question correctly), my only thought was that being that the OP wants to store the pointers captured by `op.tell()` I thought that it will make more sense to append them after capturing them instead of after (theoretically) the pointer has been moved (by `readline`, even though the `pos` variable has the previous captured one). – Zubda May 05 '19 at 13:54
  • @YHoffman it returns null value. i got empty bracket. the reason i put 2 data = op.readline() since while loop will call it – Ai Chan May 05 '19 at 13:59
  • @AiChan It's impossible to say what's the problem if you don't provive us with more information. Edit your question to have a part of your file or something. – ruohola May 05 '19 at 14:07
  • @ruohola i have provided what the data looks like – Ai Chan May 05 '19 at 14:11
1

In the comments I realized our mistake... The while data condition requires you to have read a chunk of text, I think the correct way will be to use a while True loop and break on completion.

# list to store results.
keys = []
# I used a with context manager to ensure file.close()
with open('data.txt') as f: 
    while True: 
        # read the current pointer and store it into the keys list
        pos = f.tell()
        keys.append(pos)
        # now I check if there is some data left, if not then break
        data = f.readline() 
        if not data: 
            break 

this way stores the final (trailing) pos as well if you only want the start of a row, use this

# list to store results.
keys = []
# I used a with context manager to ensure file.close()
with open('data.txt') as f: 
    while True: 
        # read the current pointer and store it into the keys list
        pos = f.tell()
        # now I check if there is some data left, if not then break
        data = f.readline() 
        if not data: 
            break
        # if we didn't break then we store the pos
        keys.append(pos)
Zubda
  • 943
  • 1
  • 6
  • 16