How can I print my dictionary in a readable format?

This code:

import csv
with open(‘cool_csv.csv’, newline=’’) as cool_csv_file:
cool_csv_dict = csv.DictReader(cool_csv_file)
print(cool_csv_dict)

prints:
<csv.DictReader object at 0x7f484e74d8d0>

How do I print out the dictionary in a readable format so that I can get a look at it?

What if it were a list? How would you print its contents?

2 Likes

I’m not sure what you’re getting at.
print(listName) works fine for a list.
for example:

lst = [1, 2, 3]
print(lst)

prints:
[1, 2, 3]

Also,
print(list(cool_csv_dict)) prints cool_csv_dict converted to a list. The thing that prints out looks more like a list of dictionaries than a dictionary. Is cool_csv_dict a dictionary or a list of dictionaries?

1 Like

Just before invoking the DictReader, run this code (it will consume the file so the code below won’t have anything; remove when done)…

  for line in cool_csv_file:
    print (line)

We see three values on each line.

Kayla Bridges,28-07-93,Has been to over fifteen different forests.

Jeremy Lopez,12-11-02,Old job was across the street from their new job.

Meredith Barker,05-07-05,Has a dog named Peanut.

William Sanchez,22-11-88,While working a phone bank accidentally called their mother.

Linda Brown,01-02-89,Can whistle the national anthem of twelve different nations.

Elizabeth Smith,23-01-75,Is triple-jointed.

They look to be values of the same type in each position. Each row is then composed as an OrderedDict, but they are all elements of a list object.

[
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.')]
), 
OrderedDict(
[('Cool Name', 'Devin Patrick'), ('Cool Birthday', '22-09-85'), ('Cool Fact', 'Happened across a major movie star while biking once.')]
), 
OrderedDict(
[('Cool Name', 'Phyllis Evans'), ('Cool Birthday', '06-02-70'), ('Cool Fact', 'Once ate three packages of cookies in one sitting.')]
), 
OrderedDict(
[('Cool Name', 'Kayla Bridges'), ('Cool Birthday', '28-07-93'), ('Cool Fact', 'Has been to over fifteen different forests.')]
), 
OrderedDict(
[('Cool Name', 'Jeremy Lopez'), ('Cool Birthday', '12-11-02'), ('Cool Fact', 'Old job was across the street from their new job.')]
), 
OrderedDict(
[('Cool Name', 'Meredith Barker'), ('Cool Birthday', '05-07-05'), ('Cool Fact', 'Has a dog named Peanut.')]
), 
OrderedDict(
[('Cool Name', 'William Sanchez'), ('Cool Birthday', '22-11-88'), ('Cool Fact', 'While working a phone bank accidentally called their mother.')]
), 
OrderedDict(
[('Cool Name', 'Linda Brown'), ('Cool Birthday', '01-02-89'), ('Cool Fact', 'Can whistle the national anthem of twelve different nations.')]
), 
OrderedDict(
[('Cool Name', 'Elizabeth Smith'), ('Cool Birthday', '23-01-75'), ('Cool Fact', 'Is triple-jointed.')]
)
]
12 Likes

It is a reader object, an example of an iterator object; it can be read through much like a list (in this case, yes, a list of dictionaries), although, as @mtf mentioned, once you go through it, it will be “exhausted.”

The exercise asks you only to iterate through it and return a certain value for each line. Just pretend that cool_csv_dict is a list of dictionaries, each having the same keys (derived from the header row of the .csv file columns), and you will be able to complete it.

2 Likes

What do you mean by “it will consume the file”?
Thanyou in advance!

The file can only be read through once, then needs to be reloaded.

1 Like

To me, DictReader looks like a list when parsed, why do we call it dictionary?

Because it still contains key-value pairs. Each row would be a single dictionary if we parsed it out of there.

So printing

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

Prints out a list of tuples that correspond to the the rows? And the tuples are like key[row] value pairs?

Sorry if that’s worded weird.

'Cool Name', 'Crystal Ellis'), ('Cool Birthday', '17-11-06'), ('Cool Fact', 'Published a small biography on a local legend.')

Parsed into a dictionary,

{
  'Cool Name': 'Crystal Ellis',
  'Cool Birthday': '17-11-06',
  'Cool Fact': 'Published a small biography on a local legend.'
}

So

[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.')])

(which is what

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

prints out)

is effectively

{
‘Cool Name’: ‘Crystal Ellis’,
‘Cool Birthday’: ‘17-11-06’,
‘Cool Fact’: ‘Published a small biography on a local legend.’
}

?

Yes, once the dictionaries are parsed out.

cool_list = []
for x in cool_csv_dict:
    cool_list.append({key: value for key, value in x})

That should give you a list of dictionaries.

2 Likes

Hi @mtf,

I tried running your code, …

import csv

with open("cool_csv.csv") as cool_csv_file:
  cool_csv_dict = csv.DictReader(cool_csv_file)
  cool_list = []
  for x in cool_csv_dict:
    cool_list.append({key: value for key, value in x})
  print(cool_list)

… but I get an error:

Traceback (most recent call last):
File “script.py”, line 7, in
cool_list.append({key: value for key, value in x})
File “script.py”, line 7, in
cool_list.append({key: value for key, value in x})
ValueError: too many values to unpack (expected 2)

Have I done something incorrectly?

Please post a link to this exercise, thanks.

Was able to get that code to work in my shell with one slight revision…

    cool_list.append({key: value for key, value in x.items()})

Will need to see my old code to find why the other no longer works. I got the same error as you before the change.