我正在阅读节点的坐标(76个节点)。基本上,我将所有坐标的字符串分开。分裂后,我有一个节点坐标的结果,第一个数字是节点的数量,因此是坐标(x,y)。示例:
['1','3600','2300']
我只想从61到末端节点获得节点的坐标。我尝试通过将节点转换为整数进行比较来使用该节点的数量。我不想删除" wher line!=" eof"行,因为它显示在文本文件的末尾。我该怎么做?
def read_coordinates(self, inputfile):
coord = []
iFile = open(inputfile, "r")
for i in range(6): # skip first 6 lines
iFile.readline()
line = iFile.readline().strip()
while line != "EOF":
values = line.split()
while int(values[0]) > 61:
coord.append([float(values[1]), float(values[2])])
line = iFile.readline().strip()
iFile.close()
return coord
您应该在 while line != "EOF":
中使用if语句喜欢:
while line != "EOF":
values = line.split()
if int(values[0]) > 61:
coord.append([float(values[1]), float(values[2])])
line = iFile.readline().strip()
替代解决方案是在孔文件中读取,然后使用列表切片删除前6行并列表理解以删除大于61的节点。
with open(iFile, 'r') as f:
coords = [ line.split(' ') for line in f] # Read every line
coords = coords[6:] # Skip first 6 lines
coords = [ [x,y] for nr, x, y in coords if int(nr) > 61] # Remove every node large r than after 61