所以我有這個程序。它記錄來自加速度計的數據。這是數據的格式。它保存在.csv文件中。在我的for循環.split()只能工作一次
time, x, y, z
0.000000,0.064553,-0.046095,0.353776
下面是在我的程序頂部聲明的變量。
length = sum(1 for line in open('couchDrop.csv'))
wholeList = [length]
timeList = [length]#holds a list of all the times
xList = [length]
yList = [length]
zList = [length]
我想創建四個列表;時間,x,y,z。目前整個數據文件存儲在一個列表中。列表中的每行包含4個數字,分別代表時間,x,y和z。我需要將wholeList分成四個不同的列表。這裏就是發生這種情況的子程序:
def easySplit():
print "easySplit is go "
for i in range (0, length):
current = wholeList[i]
current = current[:-2] #gets rid of a symbol that may be messing tings up
print current
print i
timeList[i], xList[i], yList[i], zList[i] = current.split(',')
print timeList[i]
print xList[i]
print yList[i]
print zList[i]
print ""
這是我得到的錯誤:
Traceback (most recent call last):
File "/home/william/Desktop/acceleration plotter/main.py", line 105, in <module>
main()
File "/home/william/Desktop/acceleration plotter/main.py", line 28, in main
easySplit()
File "/home/william/Desktop/acceleration plotter/main.py", line 86, in easySplit
timeList[i], xList[i], yList[i], zList[i] = current.split(',')
IndexError: list assignment index out of range`
另一個奇怪的是,我點分似乎通過循環精細工作的第一次。
任何幫助將不勝感激。