|
| 1 | +from hashlib import new |
| 2 | + |
| 3 | + |
| 4 | +contacts=[] |
| 5 | +keys=[] |
| 6 | +ncontacts=[] |
| 7 | +dic={} |
| 8 | +p=1 |
| 9 | +new_entry = [] |
| 10 | + |
| 11 | +while True: |
| 12 | + print('''\nWelcome to our contact database, please select from the following options: |
| 13 | +To create new user enter "create": |
| 14 | +To retrive an existing record enter "search": |
| 15 | +To update an existing record enter "update": |
| 16 | +To delete a record, enter "delete": |
| 17 | +When finished, enter"done":''') |
| 18 | + a = input("Enter: ") |
| 19 | + if a.lower() == 'done': |
| 20 | + False |
| 21 | + break |
| 22 | + |
| 23 | + elif a.lower() == 'create': |
| 24 | + print('\nYou selected to enter a new user follow the commands') |
| 25 | + n = input('enter a name: ') |
| 26 | + new_entry.append(n) |
| 27 | + ph = input('enter a phone number: ') |
| 28 | + new_entry.append(ph) |
| 29 | + st = input(f'enter {n}\'s state: ') |
| 30 | + new_entry.append(st) |
| 31 | + sa = input(f'enter {n}\'s spirit animal: ') |
| 32 | + new_entry.append(sa) |
| 33 | + j = ','.join(new_entry) |
| 34 | + with open('contacts.csv', 'a') as file: |
| 35 | + file.write(f'\n{j}') |
| 36 | + elif a.lower() == 'search': |
| 37 | + print('What user are you looking for?') |
| 38 | + n = input('enter name: ') |
| 39 | + with open('contacts.csv', 'r') as file: |
| 40 | + lines = file.read().split('\n') |
| 41 | + for l in lines: |
| 42 | + if n.lower() in l.lower(): |
| 43 | + print(f'User {n} found: \n---------------------------------------------\n{l}') |
| 44 | + print('---------------------------------------------') |
| 45 | + elif a.lower() == 'update': |
| 46 | + print('What contact do you want to update? ') |
| 47 | + n = input('Enter name: ') |
| 48 | + fin = open('contacts.csv', 'rt') |
| 49 | + data = fin.read() |
| 50 | + if n.lower() in data.lower(): |
| 51 | + print(f'User {n.title()} found') |
| 52 | + for lines in data.split('\n'): |
| 53 | + if n.lower() in lines.lower(): |
| 54 | + lines = lines.split(',') |
| 55 | + # lines = ','.join(lines) |
| 56 | + print(lines) |
| 57 | + chg = input(f'What needs to change, name, phone, state or spirit animal? ') |
| 58 | + if chg.lower() == 'name': |
| 59 | + chg = input('Whats the new name? ') |
| 60 | + data = data.replace(lines[0],chg) |
| 61 | + fin.close() |
| 62 | + fin = open('contacts.csv', 'wt') |
| 63 | + fin.write(data) |
| 64 | + fin.close() |
| 65 | + elif chg.lower() == 'phone': |
| 66 | + chg = input('Whats the new phone? ') |
| 67 | + data = data.replace(lines[1], chg) |
| 68 | + fin.close() |
| 69 | + fin = open('contacts.csv', 'wt') |
| 70 | + fin.write(data) |
| 71 | + fin.close() |
| 72 | + elif chg.lower() == 'state': |
| 73 | + chg = input('Whats the new state? ') |
| 74 | + data = data.replace(lines[2], chg) |
| 75 | + fin.close() |
| 76 | + fin = open('contacts.csv', 'wt') |
| 77 | + fin.write(data) |
| 78 | + fin.close() |
| 79 | + elif chg.lower() == 'spirit animal': |
| 80 | + chg = input('Whats the new spirit animal? ') |
| 81 | + data = data.replace(lines[3], chg) |
| 82 | + fin.close() |
| 83 | + fin = open('contacts.csv', 'wt') |
| 84 | + fin.write(data) |
| 85 | + fin.close() |
| 86 | + else: |
| 87 | + print('not a valid option') |
| 88 | + lines = (',').join(lines) |
| 89 | + elif a.lower() == 'delete': |
| 90 | + print('What contact do you want to delete? ') |
| 91 | + n = input('Enter name: ') |
| 92 | + fin = open('contacts.csv', 'rt') |
| 93 | + data = fin.read() |
| 94 | + if n.lower() in data.lower(): |
| 95 | + print(f'User {n.title()} found') |
| 96 | + for num, lines in enumerate(data.split('\n')): |
| 97 | + print(num, lines) |
| 98 | + if n.lower() in lines.lower(): |
| 99 | + lines = lines.split(',') |
| 100 | + # lines = ','.join(lines) |
| 101 | + print(lines) |
| 102 | + chg = input(f'Is this the entry you wish to delete?(y/n) ') |
| 103 | + |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | + |
| 108 | +with open('contacts.csv', 'r') as file: |
| 109 | + lines = file.read().split('\n') |
| 110 | + for l in lines: |
| 111 | + contacts.append(l.split(',')) #makes lists of contacts seperated by , inside of the contacts list |
| 112 | + for c in contacts[0]: #header value for key: |
| 113 | + keys.append(c) #puts the header value into a list by itself called keys |
| 114 | + # contacts.append(new_entry) |
| 115 | + while p < len(contacts): |
| 116 | + a = dic.copy() |
| 117 | + b = contacts[p].copy() |
| 118 | + for k in keys: # k is contacts[0](headers.csv) iterated over |
| 119 | + for c in b: |
| 120 | + x=b.index(c) |
| 121 | + b.pop(x) |
| 122 | + a[k] = c |
| 123 | + break |
| 124 | + p+=1 |
| 125 | + ncontacts.append(a) |
| 126 | + |
| 127 | +print(contacts) |
| 128 | +print(ncontacts) |
| 129 | + |
0 commit comments