2015-11-11 48 views
0

閱讀XY COORDS我有存儲在以下格式號的文本文件:Python從文本文件

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) 

當我運行程序我得到這個錯誤信息:

line 46 in module
polygon, rectangle = getXYCoords('HAMH HUTC XY.txt')
TypeError: init() should return None, not 'tuple'

+0

錯誤信息非常清晰...您對此有何瞭解?你有沒有理由使用類而不是函數? –

+0

我想把這段代碼放在一個單獨的python文件中,並且認爲我需要這樣做。 – goofenhour

回答

2

如果你想表示你的座標集的一類,將polygonrectangle列表設置爲實例成員,並通過self引用它們。

注意,__init__構造函數不應該返回任何東西,如果你想訪問您的polygonrectangle列出了您需要創建的getXYCoords對象(這裏稱爲coords)和訪問其成員與「點」語法: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) 
+0

謝謝你的幫助。 – goofenhour

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

+0

謝謝你的幫助。 – goofenhour