Python从文本文件中读取XY坐标

时间:2015-11-11 16:02:14

标签: python io

我有一个文本文件,其中包含以下列格式存储的数字:

93 407 77 400 94 365 109 372 
135 312 180 328
100 120 140 160

我想将这些值放入两个列表中。一个用于多边形,一个用于矩形。

如果一行中有8个数字,则会存储到多边形列表中。如果一行中有4个数字,那么它将存储在矩形列表中。像这样

polygon=[ [93, 407, 77, 400, 94, 365, 109, 372] ]

rectangle=[ [135, 312, 180, 328,], [100, 120, 140, 160] ]

然后我会使用这些值在画布上绘制矩形或多边形。

以下是我的代码到目前为止:

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)

当我运行程序时,我收到以下错误消息:

  

模块中的第46行      polygon,rectangle = getXYCoords(' HAMH HUTC XY.txt')
     TypeError: init ()应该返回None,而不是' tuple'

2 个答案:

答案 0 :(得分:2)

如果要在班级中表示坐标集,请将polygonrectangle列表设置为实例成员,并通过self引用它们。

请注意,__init__构造函数不应返回任何内容,如果您想访问polygonrectangle列表,则需要创建getXYCoords的对象(此处称为coords)并使用" dot"访问其成员。语法:coords.rectangle等:

class getXYCoords:

    def __init__(self, textFile):

        self.textFile = textFile
        self.polygon = []
        self.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]
                        self.polygon.append(line)

                    # Stores values into rectangle list
                    elif len(line) == 4:
                        line = [int(i) for i in line]
                        self.rectangle.append(line)


# Example program

if __name__ == '__main__':

    # Get XY coordinates
    coords = getXYCoords('test.txt')

    print(coords.polygon,'\n')
    print(coords.rectangle)

答案 1 :(得分:1)

在Python中,__init__()除了None之外不会返回任何内容。类的构造函数返回任何其他内容是没有意义的。在您的代码中,您似乎需要将Polygon和Rectangle作为稍后要引用的对象。如果是这样,那么您只需要将self.rectangleself.polygon作为属性添加到您的班级,并摆脱return行:

def __init__(self, textFile):

    self.textFile = textFile
    self.polygon = []
    self.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]
                    self.polygon.append(line)

                # Stores values into rectangle list
                elif len(line) == 4:
                    line = [int(i) for i in line]
                    self.rectangle.append(line)

如果您在调用班级时必须返回某些内容,则可以使用__new__()代替__init__()。有关详细信息,请参阅this answer