Using Python with CSV File Data
Put your trainer caps on, because we’re going to be reading Pokemon data––all 807 Pokemon, to be exact! In this activity, you will import a CSV file, read its data, and then search through the data for given parameters using for loops and if statements.
Starter file
# Import the necessary libraries for reading csv files
# Set the path for the csv file
# Create new lists to store data for heaviest and tallest pokemon
# Open the csv
# Iterate through the data and search for the number the user inputted. Remember to skip the header of the CSV.
# Print the name of the pokemon(identifier) and pokedex number(species id) at that number. For example, "Pokemon No. 25 - pikachu".
# Iterate through the data and search for pokemon whose weight is greater than 3000. Append only the pokemon's name and weight to the 'heaviest' list.
# Iterate through the data and search for pokemon whose height is greater than 50. Append only the pokemon's name and height to the 'tallest' list.
# Print the list of heaviest and tallest pokemon
Instructions
Open the starter file and perform the following:
- Import the necessary libraries for reading CSV files.
- Set the path for the CSV file.
- Create new lists to store data for heaviest and tallest Pokemon.
- Open the CSV file.
- Iterate through the data and search for the number the user inputted. Remember to skip the header of the CSV.
- Print the name of the Pokemon (identifier) and Pokedex number (species ID) at that number. For example, “Pokemon No. 25 – Pikachu”.
- Iterate through the data and search for Pokemon whose weight is greater than 3000. Append only the Pokemon’s name and weight to the “heaviest” list.
- Iterate through the data and search for Pokemon whose height is greater than 50. Append only the Pokemon’s name and height to the “tallest” list.
- Print the list of heaviest and tallest Pokemon.
Hint
str
and int
types cannot be evaluated against each other and must be converted to be the same data type.
Solution
# Import the necessary libraries for reading csv files
import os
import csv
# Set the path for the csv file
csvpath = os.path.join("..", "Resources", "pokemon.csv")
# Create new lists to store data for heaviest and tallest pokemon
heaviest = []
tallest = []
# Open the csv
with open(csvpath, newline="") as csvfile:
csvreader = csv.reader(csvfile, delimiter=",")
# Iterate through the data and search for the number the user inputted. Remember to skip the header of the CSV.
csv_header = next(csvreader)
for row in csvreader:
# Print the name of the pokemon(identifier) and pokedex number(species id) at that number. For example, "Pokemon No. 25 - pikachu".
print(row[1])
# Iterate through the data and search for pokemon whose weight is greater than 3000. Append only the pokemon's name and weight to the 'heaviest' list.
if int(row[4]) > 3000:
heaviest.append(row[1])
# Iterate through the data and search for pokemon whose height is greater than 50. Append only the pokemon's name and height to the 'tallest' list.
if int(row[3]) > 50:
tallest.append(row[1])
# Print the list of heaviest and tallest pokemon
print(f"Heaviest Pokemon: {heaviest}")
print(f"Tallest Pokemon: {tallest}")