1
1
def version1 ():
2
2
with open ('contacts.csv' , 'r' ) as file :
3
3
lines = file .read ().split ('\n ' )
4
- #print(lines)
5
-
4
+ contacts = []
6
5
index = 1
7
6
headers = lines [0 ].split ("," )
8
7
9
8
while index < len (lines ):
10
9
contact = lines [index ].split ("," )
11
- print (dict (zip (headers , contact )))
10
+ contacts . append (dict (zip (headers , contact )))
12
11
index += 1
12
+
13
+ return contacts
13
14
14
15
def version2 ():
15
16
16
- return 0
17
+ query_type = input ( "What would you like to do? \n Create \n Retrieve \n Update \n Delete \n > " )
17
18
19
+ if query_type == 'Create' :
20
+ with open ('contacts.csv' , 'a' ) as file :
21
+ name = input ("What is the contact's name? " )
22
+ phone = input ("What is the contact's phone? " )
23
+ email = input ("What is the contact's email? " )
24
+ file .write (f'\n { name } , { phone } , { email } ' )
25
+
26
+ elif query_type == 'Retrieve' :
27
+ contacts = version1 ()
28
+ name = input ("What is the contact's name? " )
29
+ index = 0
30
+
31
+ while index < len (contacts ):
32
+ if name == contacts [index ]['Name' ]:
33
+ return print (contacts [index ])
34
+ index += 1
35
+ else :
36
+ return print ("User not found." )
37
+
38
+ #elif query_type == 'Update':
39
+
40
+ #elif query_type == 'Delete':
41
+
42
+ #else:
43
+
18
44
def version3 ():
19
45
20
46
return 0
21
47
22
48
def main ():
23
- version1 ()
49
+ #version1()
50
+ version2 ()
24
51
25
52
main ()
0 commit comments