Address book | Sololearn: Learn to code for FREE!

0

Address book

Guys, hi. I have a problem. I made a class which help me to create contact for the address book. I've successfully made __init__ function , but I can't make remover function. I need to delete the line with the contact name and 3 subsequent lines. That's the problem. class Contact: library = 'address.txt' def __init__(self, name, number, description=''): self.name = name self.number = number self.description = description with open(Contact.library, 'a') as f: f.write(f'Имя: {self.name}\nНомер: {self.number}\nОписание: {self.description}\n\n') #name, number, description print('Контакт успешно создан*\n') #the contact was made successfully def del_contact(): import re with open(Contact.library) as f: list_of_strings = list(f) lines = f.readlines() #problem area!!! ''' deleting_contact = str(input('Введите имя удаляемого контакта: ')) #A name of delete contact pattern = re.compile(re.escape(deleting_contact)) with open(Contact.library, 'w') as f: for num, line in enumerate(lines, 1): deleting_strings = [ pattern.search(), list_of_strings[num], list_of_strings[num+1], list_of_strings[num+2] ] if line not in deleting_strings: f.write(n) ''' #problem area!!! print('Контакт успешно удалён\n') def show_all(): with open(Contact.library) as f: lines = f.readlines() for line in lines: print(line) #examples Contact('Andrew', '88005553535', '') Contact('John', '2281717') Contact.del_contact()

8/11/2020 8:02:36 AM

Андрей Пермяков

5 Answers

New Answer

+2

Андрей Пермяков , please do not place code in this size in the post directly. Please put it to playground, save it there and share a link to it in this post. Thanks!

+1

Андрей Пермяков , thanks, I've already taken a look at it for the first time. There are several questions to check before giving a recommendation. here are a few general comments: Using simple text files for storing data is a risk. Files can get corrupted, or the can loose there complete content, if they are not properly opend and closed. And it's easy to spy files if they are not encrypted. If you need the code just for learning or training purpose, this is not an issue. in any other case, information should be stored in a database. this can be sqlite3, which is supported by python module. And they should be pasword protected. If you will keep data stored in a txt file, you can use also json file format, that is easy to handle. For your current code, i would suggest to not to spread a data record on multiple lines. just put them in one line by removing '\n' from code, and therefore use a field separator like ';'. regarding the delete issue i have to go more in depth.

+1

OK. I did it for a practice. I know that sqlite3 is better for this goals. And what about your recommendation I thought about it for the begenning. Because it's more easy. So thanks anyway

0

Lothar , OK, my bad

0

I added it to my profile