0
我有一個csv文件,我想提取一些統計信息,因此我編寫了一個代碼來輸出每個用戶ID的每個活動的總數,但代碼只是正確輸出第一個ID的信息。有人告訴我什麼是我的代碼錯了嗎?這是我的代碼和錯誤的輸出。謝謝。爲什麼我的信息抽取Python代碼得到錯誤的輸出?
from collections import Counter
import csv
reader=csv.reader(file('F:\\HJZL\PythonData\log_train.csv','rb')) #read the csv file
list1=[] #create an empty list
def static(i): #information extraction function
for line in reader:
if line[0]==str(i):
list1.append(line[3]) #add activities into list
print 'ID:',i,Counter(list1) #print the statistical information of ID i
list1[:]=[] #clear the list
return list1
i=1
while i<4: #get the statistical information of ID1 to ID3
static(i)
i=i+1
print 'end'
爲什麼ID2和ID3的信息是空的?
ID1的信息在哪裏? – essramos
什麼是輸入?該輸入的實際輸出是多少?該輸入的*期望*輸出是什麼?請修改您的問題以包含該信息。另請[請閱讀如何提問問題](http://stackoverflow.com/help/how-to-ask)。 –
哦,你爲什麼要在* binary *模式下打開CSV文件?這可能會導致換行處理問題。 –