In Lab 6 - world cup - you are given a csv file with two columns of values. When reading that file, many solutions I found use csv.DictReader to read each row of the csv file as a dictionary. They go on to use "team" as a key in those dictionaries.

The first row of the csv file reads "team,rating".

My question is, does the csv.DictReader function know to call the first key "team" because that's whats written in the first row of the csv file? I can't see how else it would know what to call that key.

1 Answer 1


When in doubt read the doc.

Create an object that operates like a regular reader but maps the information in each row to a dict whose keys are given by the optional fieldnames parameter.

The fieldnames parameter is a sequence. If fieldnames is omitted, the values in the first row of file f will be used as the fieldnames. Regardless of how the fieldnames are determined, the dictionary preserves their original ordering.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .