我有一个文本文件,其编号以以下格式存储:93 407 77 400 94 365 109 372
135 312 180 328
100 120 140 160
我想把这些值放到两个列表中。一个是多边形,一个是矩形。在
如果一条线有8个数字,则存储到多边形列表中。如果一行有4个数字,那么它将被存储在矩形列表中。像这样
^{pr2}$
然后我将使用这些值在画布上绘制矩形或多边形。在
到目前为止,我的代码是: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)
当我运行程序时,我收到以下错误消息:line 46 in module
polygon, rectangle = getXYCoords('HAMH HUTC XY.txt')
TypeError: init() should return None, not 'tuple'