- [Instructor] Reading a CSV file as an array of arrays
isn't the only option.
You can also read the data into a dictionary structure,
and you can do this by using the DictReader class,
which is also in the CSV module,
so I will encourage you
to refer to the documentation link for that.
So let's open up our sample file, read_csv_dict.py,
and what we're going to do is modify our original example
to use the dictionary reader instead of the default setting.
So my starting point code defines an empty dictionary,
rather than an empty array that's going to hold the data.
So once again, we're going to open up the file in read mode.
So this should look familiar by now,
so I'll open up the file name in read mode as CSV file,
but this time,
we're going to be using the DictReader class.
So I'll type CSV DictReader and we'll pass in the CSV file.
Alright, and then we're going to loop over each row
and read one at a time,
just as we did for the array example.
So for row in reader.
Now in this case,
we're going to add the row to our dictionary,
which requires a key for the row to go into.
So I'm going to use the first value in the row as the key,
but how do I get that value?
Because remember, the row at this point is not an array,
it's a dictionary of key-value pairs.
So the DictReader class has a property called field names,
which it uses to map the values in each row,
and this property is a list
that contains the values in the CSV header row.
So field names sub zero will be given the value item name
if we look at the inventory data, right?
So this is going to be 0, 1, 2, and so on,
and we'll use that as the value,
is the key into the row data to get the next item.
It's a lot easier to see this in action,
so let's just run an experiment.
So before we do our loop,
let's just print the first row in the data.
So I'm going to write row equals next on the reader,
and then print the row,
and I'm going to also print the field names property.
So I'll call reader.fieldnames, right?
So before we actually do our loop,
let's just go ahead and run what we have.
So let's run this in the terminal.
Right, okay, so when we run this,
you can see that the first row is a dictionary
consisting of key-value pairs,
and the names property contains the values
that are used as the keys in each row.
So if I want to use the item name
as the overall dictionary key,
I just need to get the name of that key
from the field names property.
So now let's go back and write our loop.
So for each row in the reader on the data,
I'm going to get in the row,
I'm going to get the reader field names,
and I'm going to use the product name.
That's going to be at index zero is equal to the row of data.
Alright, so now this sets that row of data
into my dictionary.
I no longer need the print statements,
and I'm going to leave this next in place
'cause I want to skip over the headers.
I don't want the headers to be part of the data.
So I want to skip that row and just do the actual data rows.
I've already got my return data in place,
and as we did earlier,
let's go ahead and print out some results.
I'm going to use the Python Pretty Print module,
and rather, instead of the regular print statement
for this example,
so I'm going to write pprint.pprint,
and I'm going to print out the inventory data.
And then let's also do the same thing,
I'm going to print out the inventory data for the key of Apple,
and then let's also print out
the consumer price for the apple.
Alright.
Okay, let's save that and then let's run the code.
I'm going to run this in the terminal,
and let's make the terminal bigger.
Alright, so you can see that when I run the code,
the data is now in a dictionary, right?
And the key for each one of the values is the item name.
So starting all the way up here,
we've got avocado, bagel, baguette, banana, so on.
It looks like we're missing apple.
You know what?
Maybe it was a bad idea to leave that next statement in.
Let's go ahead and just take that out,
and let's run this again.
Alright, let's scroll all the way up.
Okay, there we go.
Apple's there now, avocado,
and you can see that each key
has this data object associated with it,
and let's scroll all the way back down to the bottom.
Right, so here at the bottom
where we accessed the specific item name
using the Apple key,
we can see that it has that particular row.
So it's got the category fruits, and so on,
and then the last line where we get the consumer price,
you can see that that's 0.75.
Now if this seems a bit complicated, okay,
you don't need to do it that way,
because remember, you can use other values for the keys.
So let's modify the code
to just use an integer for each key.
Alright, so rather than using the item name,
I'm going to make a variable called key.
I'm going to set that to zero, alright?
And then for each row in the reader,
what I'm going to do is, instead of doing this,
I'm just going to have the key, that index be the key,
and then I'm going to say key +=1,
and then obviously we need to change our print statements.
So this is no longer getting the apple.
This is getting, let use zero as the index, alright?
So let's run again.
Alright, and sure enough, now you can see
that instead of the keys being the item names,
I have these integer values,
which are now serving as the keys, right?
So we have the same result,
only now the item name is not there anymore,
but you can see that the output is essentially the same.
So it's really up to you
how you want to represent the keys in your data dictionary.