我是否正确使用file.seek和file.tell?
问题描述:
第一次在这里问一个问题,但非常难倒,可以用一只手做。我是否正确使用file.seek和file.tell?
因此,我正在为涉及论证的项目在Python中编写程序。这个脚本总的来说需要从json文件中取出一些节点,然后决定参数的线性顺序,并按顺序将它正确地放入树中。但是,我在这里遇到的主要问题是非常基本的。如果“nodeInt”等于“nodeIdentifier”,我需要将行读取器移回一行,以便从前一行(fromID)读取值。我已经强调了这些使用的地方(一个“寻求”,两个“告诉”)
不幸的是,目前它只是阅读相同的一行,似乎没有像我认为应该回来的那样。我是否错误地使用了这些命令或者是否有其他错误?提前致谢。
while addingToList == False:
if "toID" in line:
print "finding id"
print line
#Finding the ID
idStart = line.index(":")
nodeID = line[idStart+1:idStart+3]
nodeInt = int(nodeID)
if nodeInt == nodeIdentifier:
print "matching id"
**input.seek(last_pos)**
line = input.readline()
print line
fromStart = line.index(":")
fromID = line[fromStart+1:fromStart+3]
fromInt = int(fromID)
print "FromID"
print fromInt
line = input.readline()
line = input.readline()
else:
**last_pos = input.tell()**
line = input.readline()
elif "schemefulfillments" in line:
print "reached end"
addingToList = True
else:
**last_pos = input.tell()**
line = input.readline()
答
首先,我不知道,如果你刚刚不包括它,或者如果它实际上是不存在的,但是你错过了last_pos
声明你使用它作为seek
指标之前。 检查出the docs关于使用seek
和tell
以及;它应该在这里很有帮助。
是的,直到else才使用它,因为else总是出现在“if”语句之前。我会看看,谢谢 – SneakySpriggs