CSE 160 Section 7
CSV & Objects!
CSE 160: CSV & Objects
Logistics
CSE 160: CSV & Objects
Lecture Review: CSV Dictreader
CSE 160: CSV & Objects
CSV Dictreader
Say you have the following csv called people.csv with the following contents:
id,name,age,height,weight
1,Alice,20,62,120.6
2,Freddie,21,74,190.6
3,Bob,17,68,120.0
CSE 160: CSV & Objects
CSV Dictreader
Open the file by calling open and then csv.DictReader.
import csv
f = open("people.csv")
input_file = csv.DictReader(f)
f.close()
id,name,age,height,weight
1,Alice,20,62,120.6
2,Freddie,21,74,190.6
3,Bob,17,68,120.0
people.csv
CSE 160: CSV & Objects
CSV Dictreader
When you iterate over input_file, you will see each row as a dictionary:
import csv
f = open("people.csv")
input_file = csv.DictReader(f)
for row in input_file:
print(row)
f.close()
id,name,age,height,weight
1,Alice,20,62,120.6
2,Freddie,21,74,190.6
3,Bob,17,68,120.0
people.csv
CSE 160: CSV & Objects
CSV Dictreader
When you iterate over input_file, you will see each row as a dictionary:
import csv
f = open("people.csv")
input_file = csv.DictReader(f)
for row in input_file:
print(row)
f.close()
id,name,age,height,weight
1,Alice,20,62,120.6
2,Freddie,21,74,190.6
3,Bob,17,68,120.0
people.csv
Output:
{'id': '1', 'name': 'Alice', 'age': '20', 'height': '62', 'weight': '120.6'}
{'id': '2', 'name': 'Freddie', 'age': '21', 'height': '74', 'weight': '190.6'}
{'id': '3', 'name': 'Bob', 'age': '17', 'height': '68', 'weight': '120.0'}
CSE 160: CSV & Objects
CSV Dictreader
When you iterate over input_file, you will see each row as a dictionary:
import csv
f = open("people.csv")
input_file = csv.DictReader(f)
for row in input_file:
print(row)
f.close()
id,name,age,height,weight
1,Alice,20,62,120.6
2,Freddie,21,74,190.6
3,Bob,17,68,120.0
people.csv
Output:
{'id': '1', 'name': 'Alice', 'age': '20', 'height': '62', 'weight': '120.6'}
{'id': '2', 'name': 'Freddie', 'age': '21', 'height': '74', 'weight': '190.6'}
{'id': '3', 'name': 'Bob', 'age': '17', 'height': '68', 'weight': '120.0'}
Note how the values in these dictionaries are always strings!
CSE 160: CSV & Objects
Classes
CSE 160: CSV & Objects
What is a Class?
A class encapsulates and abstracts data and logic that you can use in your programs
We want to make a program that can keep track of phone numbers.
CSE 160: CSV & Objects
Classes vs. Objects
A class is a blueprint- it defines what data and behavior an object will have
We can create an object instance from a class
Class
Multiple object instances
CSE 160: CSV & Objects
A Class Contains…
Variables / fields to hold the necessary data
A phone number should probably store:
CSE 160: CSV & Objects
A Class Also Contains…
Methods/behaviors to create, query, and modify an instance of a class
(Methods are basically functions inside classes)
A phone number should probably be able to:
CSE 160: CSV & Objects
Creating an Instance of A Class
When we want to start using a class, we typically want to set some its variables to their correct values
This is done through a special method called __init__
When we create a phone number, we want to specify it’s area code, exchange, and number.
CSE 160: CSV & Objects
CSE 160: CSV & Objects
CSE 160: CSV & Objects
Section Handout Problems
CSE 160: CSV & Objects
Problem 1
CSE 160: CSV & Objects
Problem 1a
What would the following client code print?
flower_shop = Shop(“Swansons”)
flower_shop.add_item(“Daisy”)
flower_shop.print_inventory()
CSE 160: CSV & Objects
Problem 1a
What would the following client code print?
flower_shop = Shop(“Swansons”)
flower_shop.add_item(“Daisy”)
flower_shop.print_inventory()
Output:
Daisy 1
CSE 160: CSV & Objects
Problem 1b
What code would you write to make a new shop called Arcane Comics that has 5 comics in it, and then print out it's inventory?
CSE 160: CSV & Objects
Problem 1b
What code would you write to make a new shop called Arcane Comics that has 5 comics in it, and then print out it's inventory?
comic_shop = Shop("Arcane Comics")
for i in range(5):
comic_shop.add_item("Comic")
comic_shop.print_inventory()
CSE 160: CSV & Objects
Problem 1c
Adding one item at a time is extremely tedious. Write a function for the class Shop called add_items that given an item and an amount, adds that amount to the store's inventory.
CSE 160: CSV & Objects
Problem 1c
Adding one item at a time is extremely tedious. Write a function for the class Shop called add_items that given an item and an amount, adds that amount to the store's inventory.
def add_items(self, item, amount):
if item not in self.inventory:
self.inventory[item] = 0
self.inventory[item] += amount
CSE 160: CSV & Objects
Problem 2
Given the doc-strings and example output, fill in the code for each method in the StreamingService class.
CSE 160: CSV & Objects
Problem 2
Given the doc-strings and example output, fill in the code for each method in the StreamingService class.
Python Tutor
CSE 160: CSV & Objects
Problem 3
Say we have the csv file “crispra_data.csv” that came from a lab that does research on CRISPR, a gene editing/regulation tool. In this data, an experiment was performed where scientists were testing some conditions and measuring the concentration of green fluorescent protein (GFP) at various time steps.
Write a function extract_column that takes two parameters, filename and column_name. The function should return the data in the specified column from the file specified by filename. Returns the data in the column as a list.
def extract_column(filename, column_name):
"""
Returns the data in the specified column_name from the file specified by filename. Returns the data in the column as a list.
"""
# write your code here
CSE 160: CSV & Objects
Problem 3
def extract_column(filename, column_name):
"""
Returns the data in the specified column_name from the file specified by
filename. Returns the data in the column as a list.
"""
f = open(filename)
input_file = csv.DictReader(f)
result = []
for row in input_file:
result.append(float(row[column_name]))
f.close()
return result
CSE 160: CSV & Objects
Section Code: xx
CSE 160: CSV & Objects
Looking Ahead: �Data Processing
Be sure to remember the section code!
CSE 160: CSV & Objects