Reading Csv into namedtuple

python csv namedtuple

461 观看


38 作者的声誉

I am trying to load the csv file I got from here: I have rewritten this dozen of times and now I am getting error saying list index out of range. Which baffles me completely since len(row) is 15. I must be missing something obvious here.

import csv
from collections import namedtuple

fields = ('age', 

CensusRecord = namedtuple('CensusRecord', fields)

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

     for row in r:
           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()))
作者: Tummomoxie 的来源 发布者: 2017 年 12 月 27 日

回应 (2)


2382 作者的声誉

It is a syntax error in my opinion : you should do...

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

Instead of

data.append(CensusRecord(age = <your data>, ...) 
作者: IMCoins 发布者: 27.12.2017 05:05


75 作者的声誉


Open the data set with a text editor, and delete the blank lines at the end of the document. Then run your code

作者: nix 发布者: 27.12.2017 05:08