我有一個CSV文件存儲時代的學生人數已經嘗試每個問題,它的格式如下的Python:在列表或陣列存儲CSV數據
UserID Q1 Q2 Q3 Q4
20 1 2 3 1
21 0 1 2 1
我想寫一個python程序將數據存儲到數組attempts_count。
attempts_count = numpy.zeros(shape=(2000,200,200))
with open('Question_Attempts_Worksheet_1.csv' , 'r') as csvfile:
csvfile.readline() # skip the first line(column title)
for line in csvfile:
csv_row = line.split()
user_id = csv_row[0]
for question_counter in range(0,4):
attempts_count[user_id][1][question_counter] += csv_row[question_counter + 1]
我希望能獲得attempts_count[20][1][0]=1
,attempts_count[20][1][2]=3
等
但是,我得到了一個錯誤信息說
"IndexError: only integers, slices (
:
), ellipsis (...
), numpy.newaxis (None
) and integer or boolean arrays are valid indices".
可我知道我應該怎麼解決這個問題?
你有沒有考慮導入' csv'模塊?它會自動分析您的CSV文件,並允許您訪問單獨的行和列,而無需手動分析行並通過分隔符分割。 – Terry