指针位置不是从0开始

Pointer position does not start from 0

我想问一下关于 tell() 方法的问题。所以,有这样的代码

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

和结果

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

我希望我的键值从 0 开始,因为它是句子开头的第一个指针位置。但它从第二句的起始指针值开始。抱歉,我是 python.

的新手

数据看起来像这样。它包含几行

  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.

您没有将第一个指针添加到 key 列表(在执行第一个 key.append(pos) 之前,您有 2x pos = op.tell())。

您应该只删除第 2 行和第 3 行:

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

在评论中我意识到我们的错误... while data 条件要求您阅读一大段文本,我认为正确的方法是使用 while True 循环并中断完成时。

# 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 

这种方式也存储最终(尾随)pos,如果您只想要一行的开头,请使用此

# 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)