# WARM UP
def count_words(filename: str) -> int:
"""Return the number of words in the text file filename"""
count = 0
with open(filename) as myFile:
for line in myFile:
count += len(line.split())
return count
count_words("words.txt")
11
# WARM UP
import csv
def oldest_student(filename: str) -> str:
"""Return the name of the oldest student in the csv file filename"""
with open(filename) as myFile:
csvFile = csv.DictReader(myFile)
oldestName = ""
oldestAge = -1
for student in csvFile:
if int(student["Age"]) > oldestAge:
oldestAge = int(student["Age"])
oldestName = student["Name"]
return oldestName
oldest_student("Students.csv")
'Lily'
import csv
def filter_city(inFile: str, outFile: str, cityName: str) -> None:
"""Write a new file to outFile that has the information from
inFile about only the city called cityName"""
with open(inFile) as ifh:
inputCSV = csv.DictReader(ifh)
fieldnames = list(inputCSV.fieldnames or [])
with open(outFile, "w") as ofh:
outputCSV = csv.DictWriter(ofh, fieldnames)
outputCSV.writeheader()
for line in inputCSV:
if line["City"] == cityName:
outputCSV.writerow(line)
filter_city("City Data.csv", "waterloo.csv", "Waterloo")