-
Notifications
You must be signed in to change notification settings - Fork 0
/
students.py
84 lines (52 loc) · 2.43 KB
/
students.py
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
75
76
77
78
79
80
81
82
83
84
import csv
# with open("students.csv") as file:
# for line in file:
# row = line.rstrip().split(",") # split used to split a line of words with comma
# print(f"{row[0]} is in {row[1]}")
# ######## clean it a bit #########
# with open("students.csv") as file:
# for line in file:
# name, location = line.rstrip().split(",")
# print(f"{name} is in {location}")
######
# students = []
# with open("students.csv") as file:
# for line in file:
# name, house = line.rstrip().split(",")
# student = {"name": name, "house": house}
# students.append(student)
# def get_name(student): #a function of returning a name so that we can sort the names with key
# return student["name"]
# for student in sorted(students, key=get_name):
# print(f"{student['name']} is in {student['house']}")
#we can use a lambda function if we can only use a function onve
#lambda unoniymous function
# students = []
# with open("students.csv") as file:
# for line in file:
# name, house = line.rstrip().split(",")
# student = {"name": name, "house": house}
# students.append(student)
# for student in sorted(students, key=lambda student: student["name"]):
# #lambda student: students["name"] ######this line of code is equivalent to get_name function
# print(f"{student['name']} is in {student['house']}")
#################### csv reader #################\
# you'll have to import csv
# students = []
# with open("students.csv") as file:
# reader = csv.reader(file)
# for name, location, home in reader:
# students.append({"name": name, "location": location, "home": home})
# for student in sorted(students, key=lambda student: student["name"]):
# #lambda student: students["name"] ######this line of code is equivalent to get_name function
# print(f"{student['name']} is in {student['location']} at {student['home']}")
############################# use of dictreader
# students = []
# with open("students.csv") as file:
# reader = csv.DictReader(file)
# for row in reader:
# students.append({"name": row["name"], "home": row["home"]}) #
# for student in sorted(students, key=lambda student: student["name"]):
# #lambda student: students["name"] ######this line of code is equivalent to get_name function
# print(f"{student['name']} is from {student['home']}") #
################### write csv file #############