I have a text file with numbers stored in the following format:
93 407 77 400 94 365 109 372
135 312 180 328
100 120 140 160
I want to place these values into two list. One for polygons and one for rectangles.
If a line has 8 numbers in it, then gets stored to the polygon list. If a line has 4 numbers in it, then it get stored in the rectangle list. Like so
polygon=[ [93, 407, 77, 400, 94, 365, 109, 372] ]
rectangle=[ [135, 312, 180, 328,], [100, 120, 140, 160] ]
I would then uses these values to draw either a rectangle or a polygon on a canvas.
Here is what my code is so far:
class getXYCoords:
def __init__(self, textFile):
self.textFile = textFile
polygon = []
rectangle = []
with open(self.textFile) as f:
for line in f:
line = line.split() #strip EOL
if line: #lines (ie skip them)
# Stores values into polygon list
if len(line) == 8:
line = [int(i) for i in line]
polygon.append(line)
# Stores values into rectangle list
elif len(line) == 4:
line = [int(i) for i in line]
rectangle.append(line)
return polygon, rectangle
# Example program
if __name__ == '__main__':
#polygon = []
#rectangle = []
# Get XY coordinates
polygon, rectangle = getXYCoords('HAMH HUTC XY.txt')
print(polygon,'\n')
print(rectangle)
When I run the program I get this error message:
line 46 in module
polygon, rectangle = getXYCoords('HAMH HUTC XY.txt')
TypeError: init() should return None, not 'tuple'