指针位置不是从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.

在我意识到我们的错误的评论中... 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)

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

您应该删除第二行和第三行:

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

最新更新