2014-04-17 22 views
1

我有一個文本文件,其座標就像下面的例子。我的目標是加載文件並將其創建爲座標以插入海龜中以繪製某些東西,但是當我嘗試將所有東西壓碎時。傳遞參數作爲座標

super, 4 
-500, -360 
-500, 360 
500, 360 
500,-360 

sand, 29 
-400, -90 
-450, 60 
-400, 110 
-300, 110 
-250, 210 
-200, 260 

到目前爲止,我有以下代碼。我將如何使它成爲座標x和y截距。

file = open("coordinate.txt", "r") 
for line in file: 
    line_str = line.split(",") 
+4

什麼是你的錯誤輸出? – Gio

+0

可能重複的[Python:從文本文件傳遞座標到烏龜](http://stackoverflow.com/questions/23125581/python-passing-coordinates-from-text-file-to-turtle) – jonrsharpe

回答

1

你可以使用這樣

line_str=[] 
    for line in file: 
     line_str.append(line.replace('\n','').split(",")) 

 line_str.append([line.rstrip("\n").split(",")]) 
    print line_str 
+1

我更喜歡'線。 rstrip(「\ n」)';-)另外,'[int(x)for line.rstrip(「\ n」)。split(「,」)]'可能會有用。 –

+1

或者只是'line.strip()'。 – jonrsharpe

+0

謝謝大家:) –