I'm trying to make a dictionary that uses the call function 'findPokemonInfo(name)' and iterates through my csv file and returns information in this format:
attack: 49
capture_rate: 45
name: Bulbasaur
speed: 45
type: grass
The csv file is displayed like this: name,type,attack,speed,capture_rate
This is what I currently have:
def findPokemonInfo(name):
import csv
myFile = open('pokemon.csv',"r")
csvReader = csv.reader(myFile,delimiter=",")
next(csvReader)
pokeMon = {}
for row in csvReader:
if (row[0] == name):
pokeMon[row[0]] = {'attack':row[2], 'capture_rate':row[4], 'name':row[0], 'speed':row[3], 'type':row[1]}
print (pokeMon)
Which returns:
findPokemonInfo('Bulbasaur')
{'Bulbasaur': {'attack': '49', 'capture_rate': '45', 'name': 'Bulbasaur', 'speed': '45', 'type': 'grass'}}
CodePudding user response:
Print your dictionary this way:
d = {'Bulbasaur': {'attack': '49', 'capture_rate': '45', 'name': 'Bulbasaur', 'speed': '45', 'type': 'grass'}}
for key in d:
for sub_key in d[key]:
print(sub_key,": ", d[key][sub_key])
The output will be this:
attack : 49
capture_rate : 45
name : Bulbasaur
speed : 45
type : grass