-2

我想问一下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.
4

2 回答 2

1

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

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

op = open('data.txt', 'r')
key = []
while data:
    pos = op.tell()
    data = op.readline()
    key.append(pos)
于 2019-05-05T13:44:27.730 回答
1

在评论中我意识到我们的错误......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)
于 2019-05-05T14:26:24.313 回答