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!!)

94 Likes

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

3 Likes

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!

1 Like

Great elaborated explanation, thank you, highly appreciated.

1 Like

Amazing explanation - thank you!

1 Like

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

1 Like

Very well explained @patrickd314. You effort and time is much appreciated.

Oh my God, you’re the best!!

Thank you for this clear and enlightening explanation. You certainly have a great skill for explaining.

Oh! yes, I also got confused at first, but as I got to understand after reading again, the column labels are key in the dictionary. So, it’s just since as usual the third filed is ‘Email’ column, so it will be the key, and here they want to access, it’s value and append to the list created, inside the loop. Nothing just the label name, e.g.

Name : xyz
Address : 22/a xyz lane
Email : [email protected]

first field name, second address, and third is Email.
Hope I am right

@patrickd314 Very elaborate and amazing explanation as usual and especially appreciated the optional parts you shared for the deeper dive into what’s behind the scenes for DictReader. Thank you!