Michael K.
Michael K.

Reputation: 43

How to print specific key:value pairs from a pickled dictionary

I made a pickled dictionary, now I only want to retrieve the values associated with a user-inputted country.

import pickle

choice = input("Choose a country: ")
choice.capitalize()

file_name = "nationDict.dat"
fileObject = open(file_name, 'rb')
countries = pickle.load(fileObject)
for choice in countries:
    print(choice)

and I use this classmethod to create the dictionary

@classmethod
    def dictMaker(cls):
        dictCont = {}
        dictPop = {}
        dictArea = {}
        dictDensity = {}
        for i in range(193):

            dictCont[Nation.country[i]] = Nation.continent[i]
            dictPop[Nation.country[i]] = Nation.population[i]
            dictArea[Nation.country[i]] = Nation.area[i]
            dictDensity[Nation.country[i]] = Nation.density[i]


        with open("nationDict.dat", 'wb') as pickUN:
            pickle.dump((dictCont, dictPop, dictArea, dictDensity), pickUN, protocol=pickle.HIGHEST_PROTOCOL)

I want to get data only for the country of choice, but I don't understand how. I end up getting the data for every country, I do get the 4 different sets of info I want though, but I want it for only 1 country. Everything I look up is about printing entire dictionaries, but I can't find anything talking about individual values only. I've tried just about every keyword to find things on this site.

Upvotes: 0

Views: 901

Answers (2)

Alex
Alex

Reputation: 7075

I would consider storing your country data in a different form, such as nested dictionary:

import pickle 

countries = {
    Nation.country[i]: {
        "continent": Nation.continent[i],
        "population": Nation.population[i],
        "area": Nation.area[i],
        "density": Nation.density[i],
    }
    for i in range(193)
}

# Now you can pickle only one object:
with open("nation_dict.dat", "wb") as fh:
    pickle.dump(countries, fh, protocol=pickle.HIGHEST_PROTOCOL)

And your script becomes:

import pickle

choice = input("Choose a country: ")
choice.capitalize()

file_name = "nationDict.dat"
with (file_name, 'rb') as fh:
    countries = pickle.load(fileObject)

print(countries.get(choice))
# {"continent": "Europe", "population": 123456789, "area": 12345, "density": 12345}

Once your script is working I recommend posting on Code Review.

Upvotes: 2

James Grammatikos
James Grammatikos

Reputation: 490

for countryDict in countries:
    print(countryDict[choice])

Should do the trick. The variable that you have defined as countries is actually a tuple of dictionaries (dictCont, dictPop, dictArea, dictDensity). So the for loop iterates over each of those dicts and then gets the country of choice from them. In this case, countries is a poor name choice. I had read it and assumed it was a single dictionary with an array of values, as I was too lazy to read your second code block. As a rule of thumb, always assume other coders are lazy. Trust me.

Upvotes: 1

Related Questions