I have a script which i am using to parse a text file.
The script has a While loop in it as their maybe multiple next lines.
My current script is having an issue where it is skipping lines. I am pretty sure its something to do with my use of "next()" and its placement, but i cant figure it out.
This is an example of the text file:
object-group network TestNetwork1
description TestDescription
network-object host TestHost
network-object host TestHost
network-object host TestHost
network-object host TestHost
object-group network TestNetwork2
description TestDescription
network-object host TestHost
object-group network TestNetwork3
description TestDescription
network-object host TestHost
object-group network TestNetwork4
description TestDescription
network-object host TestHost
object-group network TestNetwork5
description TestDescription
network-object host TestHost
object-group network TestNetwork6
description TestDescription
network-object host TestHost
object-group network TestNetwork7
description TestDescription
network-object host TestHost
object-group network TestNetwork8
description TestDescription
network-object host TestHost
object-group network TestNetwork9
description TestDescription
network-object host TestHost
object-group network TestNetwork10s
description TestDescription
network-object host TestHost
Here is the script:
import csv
Count = 0
objects = open("test-object-groups.txt", 'r+')
iobjects = iter(objects)
with open('object-group-test.csv', 'wb+') as filename2:
writer2 = csv.writer(filename2)
for lines in iobjects:
if lines.startswith("object-group network"):
print lines
Count += 1
linesplit = lines.split()
writer2.writerow([linesplit[2]])
while True:
nextline = str(next(iobjects))
if nextline.startswith(" network-object") or nextline.startswith(" description"):
nextlinesplit = nextline.split()
if nextlinesplit[1] <> "host" and nextlinesplit[1] <> "object" and nextlinesplit[0] <> "description":
writer2.writerow(['','subnet', nextlinesplit[1], nextlinesplit[2]])
elif nextlinesplit[1] == "host":
writer2.writerow(['',nextlinesplit[1], nextlinesplit[2]])
elif nextlinesplit[1] == "object":
writer2.writerow(['',nextlinesplit[1], nextlinesplit[2]])
elif nextlinesplit[0] == "description":
writer2.writerow(['',nextlinesplit[0]])
elif nextline.startswith("object-group"):
break
print Count
Here is the output showing that it is skipping lines:
object-group network TestNetwork1
object-group network TestNetwork3
object-group network TestNetwork5
object-group network TestNetwork7
object-group network TestNetwork9
5
As you can see above, the line items are skipping.
Any idea how to fix this?