Skip to content

Commit 7a58d80

Browse files
authored
Merge pull request #126 from PdxCodeGuild/jonpan-lab11-fileio-v2
Submitting lab11
2 parents 12e7dab + 9bd402d commit 7a58d80

File tree

4 files changed

+139
-0
lines changed

4 files changed

+139
-0
lines changed

code/jonpan/contacts.csv

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
id,first_name,last_name,email,gender,ip_address
2+
1,Cello,Lorenzo,[email protected],Male,61.139.122.253
3+
2,Felita,Lawford,[email protected],Female,103.62.148.69
4+
3,Gene,Waterson,[email protected],Female,177.133.78.160
5+
4,Vitia,Cumbers,[email protected],Female,242.19.105.246
6+
5,Dalston,Smitham,[email protected],Male,253.115.85.93

code/jonpan/contacts2.csv

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
name,email,phone
2+
cello,[email protected],432-412-3921
3+
felita,[email protected],103-621-4869
4+
gene,[email protected],177-133-7816
5+
vitia,[email protected],242-191-0524
6+
dalston,[email protected],253-115-8593

code/jonpan/lab11_v1.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Lab11 v1
2+
3+
with open('contacts.csv', 'r') as file: # open the file
4+
lines = file.read().split('\n') # when encounter a new line character, store into lines variable
5+
6+
header = lines[0].split(',')
7+
8+
contacts_list = []
9+
10+
for line in lines:
11+
contacts_dict = {}
12+
elements = line.split(',')
13+
for i in range(len(header)):
14+
contacts_dict.update({header[i]: elements[i]})
15+
contacts_list.append(contacts_dict)
16+
17+
contacts_list.pop(0)
18+
19+
for lines in contacts_list:
20+
print(lines)

code/jonpan/lab11_v2.py

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# Lab 11, this combines version 2 and 3
2+
3+
class ContactList:
4+
5+
contacts = {}
6+
7+
def __init__(self):
8+
9+
with open('contacts2.csv', 'r') as file:
10+
lines = file.read().split('\n')
11+
12+
header = lines[0].split(',')
13+
14+
contacts_list = []
15+
16+
for line in lines:
17+
contacts_dict = {}
18+
elements = line.split(',')
19+
for i in range(len(header)):
20+
contacts_dict.update({header[i]: elements[i]})
21+
contacts_list.append(contacts_dict)
22+
contacts_list.pop(0)
23+
self.contacts = contacts_list
24+
25+
def create_record(self):
26+
name = input("What is the name?")
27+
email = input("What is the email?")
28+
phone = input("what is the phone number?")
29+
30+
newrecord = {"name": name,
31+
"email": email,
32+
"phone": phone
33+
}
34+
35+
self.contacts.append(newrecord)
36+
37+
with open('contacts2.csv', 'a') as file:
38+
file.write(f'\n{name},{email},{phone}')
39+
40+
def print_list(self):
41+
print(self.contacts)
42+
43+
44+
def retrieve_record(self):
45+
search_name = input("What name are you searching for?")
46+
for i in self.contacts:
47+
if i['name'].lower() == search_name.lower():
48+
print(i)
49+
50+
def update_record(self):
51+
search_name = input("Which name do you want to update?")
52+
updated_attribute = input("Which field do you want to update? Options are name, email, phone.")
53+
change_attribute = input("What do you want to update the field to be?")
54+
for i in self.contacts:
55+
if i['name'].lower() == search_name.lower():
56+
i[updated_attribute.lower()] = change_attribute
57+
58+
# print(self.contacts)
59+
60+
with open('contacts2.csv', 'w') as file:
61+
file.write("name,email,phone\n")
62+
for i in self.contacts:
63+
name = i["name"]
64+
email = i["email"]
65+
phone = i["phone"]
66+
file.write(f'{name},{email},{phone}\n')
67+
68+
def delete_record(self):
69+
search_name = input("Which contact do you want to delete? Enter their name")
70+
for i in self.contacts:
71+
if i['name'].lower() == search_name.lower():
72+
self.contacts.remove(i)
73+
74+
with open('contacts2.csv', 'w') as file:
75+
file.write("name,email,phone\n")
76+
for i in self.contacts:
77+
name = i["name"]
78+
email = i["email"]
79+
phone = i["phone"]
80+
file.write(f'{name},{email},{phone}\n')
81+
82+
contacts_test = ContactList()
83+
while True:
84+
command = input('Enter a command: ')
85+
if command == 'retrieve':
86+
contacts_test.retrieve_record()
87+
print("Record retrieved")
88+
elif command == 'create':
89+
contacts_test.create_record()
90+
print("Record created")
91+
elif command == 'update':
92+
contacts_test.update_record()
93+
print("Record updated")
94+
elif command == 'delete':
95+
contacts_test.delete_record()
96+
print("Record deleted")
97+
elif command == 'help':
98+
print('Available commands:')
99+
print('retrieve - retrieve a record')
100+
print('create - create a record')
101+
print('update - update a record')
102+
print('delete - delete a record')
103+
print('exit - exit the program')
104+
elif command == 'exit':
105+
break
106+
else:
107+
print('Command not recognized')

0 commit comments

Comments
 (0)