【发布时间】:2017-09-12 14:33:03
【问题描述】:
所以我有这个应用程序。它记录来自加速度计的数据。这是数据的格式。它保存在 .csv 文件中。
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`
另一个奇怪的事情是我的点分割似乎在第一次通过循环时工作正常。
任何帮助将不胜感激。
【问题讨论】:
标签: python list loops csv split