2017-12-27 482 views
0

我想加載我從這裏得到的csv文件:http://archive.ics.uci.edu/ml/machine-learning-databases/adult/adult.data我已經重寫了這十幾次,現在我得到錯誤說列表索引超出範圍。自len(row)是15以來,這完全讓我感到困惑。我必須在這裏忽略一些明顯的東西。閱讀Csv到namedtuple

import csv 
from collections import namedtuple 

fields = ('age', 
     'workclass', 
     'fnlwgt', 
     'education', 
     'education_num', 
     'marital_status', 
     'occupation', 
     'relationship', 
     'race', 
     'sex', 
     'capital_gain', 
     'capital_loss', 
     'hours_per_week', 
     'native_country', 
     'target') 

CensusRecord = namedtuple('CensusRecord', fields) 

with open("./data/adult_data.csv","r") as f: 
    r = csv.reader(f, delimiter=',') 

    for row in r: 
      data.append(CensusRecord(
      age    = int(row[0]), 
      workclass  = row[1].strip(), 
      fnlwgt   = float(row[2].strip()), 
      education  = row[3].strip(), 
      education_num = int(row[4]), 
      marital_status = row[5].strip(), 
      occupation  = row[6].strip(), 
      relationship  = row[7].strip(), 
      race    = row[7].strip(), 
      sex    = row[9].strip(), 
      capital_gain  = int(row[10]), 
      capital_loss  = int(row[11]), 
      hours_per_week = int(row[12]), 
      native_country = row[13].strip(), 
      target   = row[14].strip())) 

回答

1

打開數據用文本編輯器設置,並刪除文檔末尾的空白行。然後運行你的代碼

+0

你是英雄。非常感謝你 :) – Tummomoxie

0

這在我看來是一個語法錯誤:你應該做的......

data.append(CensusRecord("age" = <your_data>, ...) 

而不是

data.append(CensusRecord(age = <your data>, ...) 
+0

謝謝,但求助nix找到了什麼問題。這是我的csv底部的空行。 – Tummomoxie