出於某種原因,我正在嘗試當我刪除了x.append和y.append線,只是把一個print語句運行下面的代碼指數誤差超出範圍蟒蛇的
#!/usr/bin/python
import matplotlib.pyplot as plt
x = []
y = []
readFile = open('Out_0_0.txt','r')
sepFile = readFile.read().split('\n')
readFile.close()
for plotPair in sepFile:
xandy = plotPair.split()
x.append(int(xandy[0]))
y.append(int(xandy[1]))
print x
print y
當這個錯誤打印出xandy中的內容,它會打印出數組中的每對值。該文本文件有這個
1 2
3 4
5 6
7 8
所有我希望它做的是存儲在X數組中的第一列和Y數組
你爲什麼要將新閱讀分爲新行? '.read'會自動讀取一行中的一行。你可能是想要做'.readlines'? –
你有什麼錯誤?當我運行代碼時,我得到'x = [1,3,5,7]'和'y = [2,4,6,8]' – Andy
我猜測(不確定)你是否有空行在文件的底部。 – NightShadeQueen