I am trying to parse a file, in which there is a part always present, and the past part is optional.
for line in finp:
# This part is always present
for _ in range(int(ldata[2])):
sdata = finp.readline()
tdos.write(sdata)
#This part may or may not be present
for i in range(int(atoms)):
next(finp)
for j in range(int(ldata[2])):
aatom[i][j] = [float(x) for x in
finp.readline().strip().split()]
The problem is, if the optional part is not present, next(finp)
is giving error:
next(finp)
StopIteration
I have tried with:
for i in range(int(atoms)):
if i is not None:
next(finp)
for j in range(int(ldata[2])):
aatom[i][j] = [float(x) for x in
finp.readline().strip().split()]
else:
break
But that does not solve the problem. I have found many previous question with same question like this, but unable to solve this problem.
Is the only way to solve it, as stated in the accepted ans is to read the whole file at once and then process?