What does the example mean by using the third field "email" as a key in a dictionary?

" Since our CSV’s first line calls the third field in our CSV “ Email “, we can use that as the key in each row of our DictReader."

I’m having trouble understanding what the above quotation means.

How is the first line calling the third field?

In the solution no key arguments are specified for DictReader:

cool_csv_dict = csv.DictReader(cool_csv_file)

Hi, @petercook0108566555 Don’t worry about the word “call.” That sentence could be re-written:

Since our CSV’s first line identifies the third field in our CSV “ Email “, we can use that as the key in each row of our DictReader.

If you print out the original file:

with open('cool_csv.csv') as cool_csv_file:
  for line in cool_csv_file:
    print(line.strip())

You will see:

Cool Name,Cool Birthday,Cool Fact  
Trevor Torres,03-09-08,Has never been out of the country.
Crystal Ellis,17-11-06,Published a small biography on a local legend.
... etc.

That first line, Cool Name,Cool Birthday,Cool Fact , is the header line, used by csv.DictReader to obtain the keys for its output dictionaries.

So “Cool Fact” is actually the header or field name that we’re interested in, and that is what is used in the solution:

  for row in cool_csv_dict:
    print(row['Cool Fact'])

Entirely optional below this line:


You can further investigate what’s going on:

with open('cool_csv.csv') as cool_csv_file:
  cool_csv_dict = csv.DictReader(cool_csv_file)
  print(cool_csv_dict)

Output:

<csv.DictReader object at 0x7f941b527748>

Hmm, not too useful. What’s inside that object?

with open('cool_csv.csv') as cool_csv_file:
  cool_csv_dict = csv.DictReader(cool_csv_file)  )
  for row in cool_csv_dict:
    print(row)

Output:

OrderedDict([('Cool Name', 'Trevor Torres'), ('Cool Birthday', '03-09-08'), ('Cool Fact', 'Has never been out of the country.')])
OrderedDict([('Cool Name', 'Crystal Ellis'), ('Cool Birthday', '17-11-06'), ('Cool Fact', 'Published a small biography on a local legend.')])
... etc.

Well! That’s a bit strange! We haven’t covered the OrderedDict type, but if you look at it a bit, you will see that each entry is a list of tuples, and that each tuple looks like a key:value pair from a conventional dictionary, the final key being “Cool Fact” in each case.

If OrderedDict is too much to deal with right now, you can cast it to a conventional dict by changing that last line, print(row) to print(dict(row)), and then the output looks like this:

{'Cool Name': 'Trevor Torres', 'Cool Birthday': '03-09-08', 'Cool Fact': 'Has never been out of the country.'}
{'Cool Name': 'Crystal Ellis', 'Cool Birthday': '17-11-06', 'Cool Fact': 'Published a small biography on a local legend.'}

So, That is what csv.DictReader does: it takes this:

Cool Name,Cool Birthday,Cool Fact
Trevor Torres,03-09-08,Has never been out of the country.
Crystal Ellis,17-11-06,Published a small biography on a local legend.
... etc

… and turns it into this:

{'Cool Name': 'Trevor Torres', 'Cool Birthday': '03-09-08', 'Cool Fact': 'Has never been out of the country.'}
{'Cool Name': 'Crystal Ellis', 'Cool Birthday': '17-11-06', 'Cool Fact': 'Published a small biography on a local legend.'}
... etc

… or, from the docs:

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

(And that’s nothing compared to what Pandas can do!!)

31 Likes

Jeez. Not sure if I’m excited for Pandas :see_no_evil: Thx again!

1 Like

So the best way to think about this is that each line has been zipped with the header line to create a standalone dictionary… Correct?

Thanks. This was really very helpful!

Great elaborated explanation, thank you, highly appreciated.

Amazing explanation - thank you!

Thanks for taking out time & writing such a detailed explanation. Helped me a lot.