-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsv_notes_finished.py
More file actions
71 lines (62 loc) · 2.94 KB
/
csv_notes_finished.py
File metadata and controls
71 lines (62 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# Notes on file I/O with CSV files
# CSV file (.csv) - comma-separated values;
# common format for sending tabular data (rows & columns) between
# programs (e.g., Excel or Google Sheets)
with open('notes.csv', 'r') as csv_file:
data = csv_file.readlines()
# String methods for working with strings:
# .rstrip() can remove trailing characters from strings
# .strip() can remove leading and trailing characters from strings
# .split() is a string method that can separate out a string into a list
# using a default separator (a.k.a. delimiter).
# By default, split uses spaces as the separator.
# You can change the separator by passing the new
# separator as a argument:with
# i.e. split(",") for CSV file data
for line in data:
# TEACHER NOTE: print line first to see the newline characters
stripped_line = line.strip()
#print(line) #print(stripped_line)
row_list = stripped_line.split(",")
#print(row_list)
column_a = row_list[0].strip()
column_b = row_list[1].strip()
column_c = row_list[2].strip()
print(f"|{column_a:<25}|{column_b:^25}|{column_c:>25}|")
'''
f-strings - new string formatting technique in Python 3.6+
f'xxxxxxxxx{expression}xxxxxxxxxxx'
aligning with f-strings:
left-align = f'xxxxxxxxx{expression:<15}xxxxxxxxxx'
center-align = f'xxxxxxxxx{expression:^15}xxxxxxxxxx'
right-align = f'xxxxxxxxx{expression:>15}xxxxxxxxxx'
'''
# working with custom data in csv files
# filter/ analysis on the data in College_Data.csv
def print_data(data):
"""Display the data in College_Data.csv in a formatted table"""
#print(f"|{'College':<50}|{'Out-of-State Tuition':^20}|{'Graduation Rate':^20}|")
for line in data[1:25]: # skip the header row and only show the first 25 rows of data
line = line.strip().split(",")
print(f"|{line[0]:<50}|${line[9]:^20}|{line[-1]:^20}%|")
# filter/analyze the data in College_Data.csv to find the schools with:
# <50% acceptance rate
# <$30,000 oos tuition
# >75% graduation rate
# print the name of the school and data in a formatted table
def analyze_data(data):
#print(f"|{"College":<50}|{"Acceptance Rate":>25}|{"Out-of-State Tuition":>25}|{"Graduation Rate":>25}|")
for line in data[1:]: # skip the header row
line = line.strip().split(",")
school_name = line[0]
acceptance_rate = (float(line[3])/float(line[2]) * 100)
acceptance_rate = round(acceptance_rate, 2)
oos_tuition = float(line[9])
graduation_rate = float(line[-1])
if acceptance_rate < 50 and oos_tuition < 30000 and graduation_rate > 75:
print(f"|{school_name}|")
#print(f"|{school_name[:25]:<25}|{acceptance_rate:^25.2f}%|${oos_tuition:^25,.2f}|{graduation_rate:^25.2f}%|")
with open("College_Data.csv", "r") as data:
data_list = data.readlines()
print_data(data_list)
analyze_data(data_list)