我想问一下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.
答案 0 :(得分:1)
您没有将第一个指针添加到key
列表中(在执行第一个pos = op.tell()
之前有2个key.append(pos)
)。
您应该删除第二行和第三行:
op = open('data.txt', 'r')
key = []
while data:
pos = op.tell()
data = op.readline()
key.append(pos)
答案 1 :(得分: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
这样,如果您只想要一行的开始,也可以存储最后一个{train} 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)