Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a function which reads in a csv file, and then returns a dictionary where keys are (standardized) titles and values are their frequencies in

Write a function which reads in a csv file, and then returns a dictionary where keys are (standardized) titles and values 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

Note that there is an unintentional typo in the dataset. Your code needs to account for that.

Do not 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_titles(csv_file_name):

#write the code to be placed here:

try: titlecounts = count_titles('faculty.csv') os.remove('faculty.csv') except Exception as e: os.remove('faculty.csv') raise(e) titlecounts = { str(key).replace(' ', '').replace('.', '').lower()[:9]: val for key, val in titlecounts.items() }

titles = ['professor', 'associate', 'assistant'] assert len(titles) >= len(titlecounts), 'did you get all the different titles?' assert len(titles) == len(titlecounts), 'your output has too many titles' for title in titles: count = titlecounts.get(title, -1) print(count)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Systems For Advanced Applications 15th International Conference Dasfaa 2010 Tsukuba Japan April 2010 Proceedings Part 1 Lncs 5981

Authors: Hiroyuki Kitagawa ,Yoshiharu Ishikawa ,Wenjie Li ,Chiemi Watanabe

2010th Edition

3642120253, 978-3642120251

More Books

Students also viewed these Databases questions