Question
Write a function which reads in a csv file, and then returns a dictionary where keys are (standardized) degrees and calues are their frequencies in
Write a function which reads in a csv file, and then returns a dictionary where keys are (standardized) degrees and calues are their frequencies in the file.
Your function will be tested using the csv file located at: https://raw.githubusercontent.com/thisismetis/dsp/master/python/faculty.csv
Use regular expressions so that your code considers "PhD" and "Ph.D." to be the same string
Do noe use pandas!
#!/bin/python3
import sys import os
facultycsv = """name, degree, title, email Scarlett L. Bellamy, Sc.D.,Associate Professor of Biostatistics,bellamys@mail.med.upenn.edu Warren B. Bilker,Ph.D.,Professor of Biostatistics,warren@upenn.edu Matthew W Bryan, PhD,Assistant Professor of Biostatistics,bryanma@upenn.edu Jinbo Chen, Ph.D.,Associate Professor of Biostatistics,jinboche@upenn.edu Susan S Ellenberg, Ph.D.,Professor of Biostatistics,sellenbe@upenn.edu Jonas H. Ellenberg, Ph.D.,Professor of Biostatistics,jellenbe@mail.med.upenn.edu Rui Feng, Ph.D,Assistant Professor of Biostatistics,ruifeng@upenn.edu Benjamin C. French, PhD,Associate Professor of Biostatistics,bcfrench@mail.med.upenn.edu Phyllis A. Gimotty, Ph.D,Professor of Biostatistics,pgimotty@upenn.edu Wensheng Guo, Ph.D,Professor of Biostatistics,wguo@mail.med.upenn.edu Yenchih Hsu, Ph.D.,Assistant Professor of Biostatistics,hsu9@mail.med.upenn.edu Rebecca A Hubbard, PhD,Associate Professor of Biostatistics,rhubb@mail.med.upenn.edu Wei-Ting Hwang, Ph.D.,Associate Professor of Biostatistics,whwang@mail.med.upenn.edu Marshall M. Joffe, MD MPH Ph.D,Professor of Biostatistics,mjoffe@mail.med.upenn.edu J. Richard Landis, B.S.Ed. M.S. Ph.D.,Professor of Biostatistics,jrlandis@mail.med.upenn.edu Yimei Li, Ph.D.,Assistant Professor of Biostatistics,liy3@email.chop.edu Mingyao Li, Ph.D.,Associate Professor of Biostatistics,mingyao@mail.med.upenn.edu Hongzhe Li, Ph.D,Professor of Biostatistics,hongzhe@upenn.edu A. Russell Localio, JD MA MPH MS PhD,Associate Professor of Biostatistics,rlocalio@upenn.edu Nandita Mitra, Ph.D.,Associate Professor of Biostatistics,nanditam@mail.med.upenn.edu Knashawn H. Morales, Sc.D.,Associate Professor of Biostatistics,knashawn@mail.med.upenn.edu Kathleen Joy Propert, Sc.D.,Professor of Biostatistics,propert@mail.med.upenn.edu Mary E. Putt, PhD ScD,Professor of Biostatistics,mputt@mail.med.upenn.edu Sarah Jane Ratcliffe, Ph.D.,Associate Professor of Biostatistics,sratclif@upenn.edu Michelle Elana Ross, PhD,Assistant Professor is Biostatistics,michross@upenn.edu Jason A. Roy, Ph.D.,Associate Professor of Biostatistics,jaroy@mail.med.upenn.edu Mary D. Sammel, Sc.D.,Professor of Biostatistics,msammel@cceb.med.upenn.edu Pamela Ann Shaw, PhD,Assistant Professor of Biostatistics,shawp@upenn.edu Russell Takeshi Shinohara,0,Assistant Professor of Biostatistics,rshi@mail.med.upenn.edu Haochang Shou, Ph.D.,Assistant Professor of Biostatistics,hshou@mail.med.upenn.edu Justine Shults, Ph.D.,Professor of Biostatistics,jshults@mail.med.upenn.edu Alisa Jane Stephens, Ph.D.,Assistant Professor of Biostatistics,alisaste@mail.med.upenn.edu Andrea Beth Troxel, ScD,Professor of Biostatistics,atroxel@mail.med.upenn.edu Rui Xiao, PhD,Assistant Professor of Biostatistics,rxiao@mail.med.upenn.edu Sharon Xiangwen Xie, Ph.D.,Associate Professor of Biostatistics,sxie@mail.med.upenn.edu Dawei Xie, PhD,Assistant Professor of Biostatistics,dxie@upenn.edu Wei (Peter) Yang, Ph.D.,Assistant Professor of Biostatistics,weiyang@mail.med.upenn.edu"""
with open('faculty.csv', 'w') as f: f.write(facultycsv)
# Complete the function below.
def count_degrees(csv_file_name):
try: degreecounts = count_degrees('faculty.csv') os.remove('faculty.csv') except Exception as e: os.remove('faculty.csv') raise(e) degreecounts = { str(key).replace(' ', '').replace('.', '').upper(): val for key, val in degreecounts.items() }
degrees = ['MD', 'MA', 'SCD', 'BSED', 'PHD', '0', 'MPH', 'MS', 'JD'] assert len(degrees) >= len(degreecounts), 'did you get all the different degrees?' assert len(degrees) == len(degreecounts), 'your output has too many degrees' for degree in degrees: count = degreecounts.get(degree, -1) print(count)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started