Reputation: 11
I'm working on a python code and I get this error: "TypeError: new() missing 3 required positional arguments: 'name', 'freq', and 'gen'"
I'm importing a csv file to create a list of tuples, using a namedtuple.
import csv
from collections import namedtuple
Rec = namedtuple('Rec', 'year, name, freq, gen')
def read_file(file):
with open(file) as f:
reader = csv.reader(f)
next(reader)
for line in reader:
recs= Rec(line)
return recs
read_file("./data/file.csv")
It's probably some newbie problem, but that's what I am :) I'd appreciate any help!
Upvotes: 0
Views: 1663
Reputation: 500853
line
is a tuple. When you call Rec(line)
, that entire tuple gets interpreted as the year
argument (with the other three arguments being missing, hence the error).
To fix this, change
recs = Rec(line)
to
recs = Rec(*line)
or
recs = Rec._make(line)
https://docs.python.org/2/library/collections.html#collections.somenamedtuple._make
Upvotes: 2