Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create task2 #48

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions task2
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import re
from pprint import pprint
import csv

PHONE_PATTERN = r'(\+7|8)*[\s\(]*(\d{3})[\)\s-]*(\d{3})[-]*(\d{2})[-]*(\d{2})[\s\(]*(доб\.)*[\s]*(\d+)*[\)]*'
PHONE_SUB = r'+7(\2)-\3-\4-\5 \6\7'

with open("phonebook_raw.csv", encoding="utf-8") as f:
rows = csv.reader(f, delimiter=",")
contacts_list = list(rows)

def main(contact_list: list):
"""основная логика"""
new_list = list()
for item in contact_list:
full_name = ' '.join(item[:3]).split(' ')
result = [full_name[0], full_name[1], full_name[2], item[3], item[4],
re.sub(PHONE_PATTERN, PHONE_SUB, item[5]),
item[6]]
new_list.append(result)
return union(new_list)

def union(contacts: list):
for contact in contacts:
first_name = contact[0]
last_name = contact[1]
for new_contact in contacts:
new_first_name = new_contact[0]
new_last_name = new_contact[1]
if first_name == new_first_name and last_name == new_last_name:
if contact[2] == "": contact[2] = new_contact[2]
if contact[3] == "": contact[3] = new_contact[3]
if contact[4] == "": contact[4] = new_contact[4]
if contact[5] == "": contact[5] = new_contact[5]
if contact[6] == "": contact[6] = new_contact[6]

result_list = list()
for i in contacts:
if i not in result_list:
result_list.append(i)

return result_list

with open("phonebook.csv", "w", encoding="utf-8") as f:
datawriter = csv.writer(f, delimiter=',')
datawriter.writerows(main(contacts_list))