-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSectionFilter.py
More file actions
74 lines (65 loc) · 2.61 KB
/
SectionFilter.py
File metadata and controls
74 lines (65 loc) · 2.61 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
72
73
74
# This program filters students based on user selected section number.
import os, re
COURSE_NAME = "EEE3308C"
TABLE_CATEGORIES = "Name,Breadboard,Power Supply,Parts"
def extractCVSInfo(namePattern):
print("Looking for valid files in current directory...")
csvFiles = [file for file in os.listdir('.') if re.match(namePattern,file)]
if len(csvFiles) > 1:
print("Multiple " + COURSE_NAME + " CSV files in the current directory: ", csvFiles)
print("Please make sure there is only one " + COURSE_NAME + " CSV file and try again.")
exit()
elif len(csvFiles) == 0:
print("No " + COURSE_NAME + " CSV file found in current directory. Please try again.")
exit()
try:
with open(csvFiles[0], "r") as studentInfo:
lines = [re.split(r',|\n',row) for row in studentInfo]
except EnvironmentError:
print("Failed to open the CSV file: ",csvFiles[0])
print("Please check the file and try again.")
exit()
print("The following valid file found:",csvFiles[0])
return lines
def select_section(sections):
selection = 0
print("Section numbers found in file:")
for index in range(len(sections)):
print(index + 1,") ",sections[index],sep="")
while True:
try:
selection = int(input("Please select a section: "))
if selection < 1 or selection > len(sections):
raise RuntimeError
break
except:
print("Invalid input. Try again.")
return sections[selection - 1]
def main():
namePattern = r"\d+_\w+_\d+_\d+_Grades-" + COURSE_NAME + r"\.csv"
lines = extractCVSInfo(namePattern)
filtered_list = []
course_sections = []
for row in lines:
for entry in row:
if "Student" in entry: # erase test student
lines.remove(row)
break
if COURSE_NAME + "-" in entry and entry not in course_sections:
course_sections.append(entry)
section = select_section(list(course_sections))
for row in lines:
for entry in row:
if section in entry:
filtered_list.append(row)
output_name = str(section) + "_students.csv"
with open(output_name,'w') as output_file:
print(section,"\n,",file=output_file)
print(TABLE_CATEGORIES,file=output_file)
for row in filtered_list:
row = list(row)
print(row[0],row[1],sep=",",file=output_file)
print("\n\nTOTAL:",len(filtered_list),file=output_file)
print("Done. The following filtered file created:",output_name)
if __name__ == "__main__":
main()