Last active
February 23, 2018 20:22
-
-
Save AO8/90d5317371f0129b166a71f4a359155a to your computer and use it in GitHub Desktop.
Python program to analyze data from IE. Quantifies non-IT enrollments generated by BAS-SD for other departments on campus.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # AO8 | |
| # February 16, 2018 | |
| import csv | |
| import os | |
| import re | |
| def build_csv(): | |
| """iterates through CSV files in directory to create master gen_ed_data.csv""" | |
| rows = [] | |
| for filename in os.listdir(directory): | |
| if filename.endswith(".csv"): | |
| with open(filename, "r") as f: | |
| contents = f.readlines() | |
| for line in contents: | |
| line = line.split(",") | |
| rows.append(line) | |
| for row in rows: | |
| if "\n" in row[2]: | |
| row[2] = re.sub("\n", "", row[2]) | |
| with open("gen_ed_data.csv", "w", newline="") as f: | |
| writer = csv.writer(f) | |
| for row in rows: | |
| writer.writerow(row) | |
| def read_csv(filename): | |
| """opens and reads CSV file to create a list of rows""" | |
| with open(filename, "r") as f: | |
| reader = csv.reader(f) | |
| rows = [] | |
| for row in reader: | |
| rows.append(row) | |
| return rows | |
| def convert_to_int(lst): | |
| """converts row[1] from str to int""" | |
| for i in lst: | |
| i[2] = int(i[2]) | |
| return lst | |
| def remove_amper(lst): | |
| """removes ampersands from course prefixes""" | |
| for i in lst: | |
| if "&" in i[0]: | |
| i[0] = i[0][0:-1] # remove ampersand from course prefix | |
| return lst | |
| def create_dict(lst): | |
| """creates a dictionary from a list of rows""" | |
| d = {} | |
| for i in lst: | |
| if i[0] == "IT" or i[0] == "STEPP": # disregards IT and STEPP enrollments | |
| continue | |
| elif i[0] in d: | |
| d[i[0]] += i[2] | |
| else: | |
| d[i[0]] = i[2] | |
| return d | |
| def calc_total_enrollments(lst): | |
| totals = [] | |
| for i in lst: | |
| if i[0] == "IT" or i[0] == "STEPP": | |
| continue | |
| else: | |
| totals.append(i[2]) | |
| s = sum(totals) | |
| return s | |
| def sort_dict(d): | |
| """sorts dict by values""" | |
| sorted_d = [(k, d[k]) for k in sorted(d, key=d.get, reverse=True)] | |
| return sorted_d | |
| def display_gened_info(filename): | |
| """prints information about the sorted dictionary""" | |
| r = read_csv(filename) | |
| a = remove_amper(r) | |
| c = convert_to_int(a) | |
| d = create_dict(c) | |
| s = calc_total_enrollments(c) | |
| sorted_d = sort_dict(d) | |
| print("\n***\n") | |
| for k, v in sorted_d: | |
| print(f"{k} = {v}") | |
| print(f"\nBAS in Software Development generated {s} non-IT enrollments\nfor {len(sorted_d)} departments on campus!\n") | |
| build_csv() | |
| display_gened_info("gen_ed_data.csv") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment