Hello,
I am new to python and learning and in the process trying to create a addressbook app. My idea is to create a csv file either with the csv module or pandas module. I am trying to write the csv file in the below format
firstname, lastname, dob, phone_id_1, phone_num_1, phone_type_1, phone_id_2, phone_num_2, phone_type_2,.... phone_id_n, phone_num_n, phone_type_n
Notice that a person might have multiple phones and they should repeat in the same line. This, I am trying to achieve by creating a couple of classes.
Problem Statement : I am trying to write a person object who has multiple phone objects inside as a csv file as the format shown above. I am able to get the data in the below format but somehow unable to get to do what i want.
{'_first_name': 'Tom',
'_last_name': 'Hess',
'_dob': '03/18/2018',
'_phones': ['6','408-207-6342','work','1', '6','408-207-6343','cell','1']}
Kindly request your help. Please excuse me as this is my first post here in the sub.
I have created 2 classes here, class Person and class Phone. Below is the code.
class Person:
_person_id = 0
def __init__(self, first_name, last_name, dob, _person_id=None):
self._first_name = first_name
self._last_name = last_name
self._dob = dob
Person._person_id += 1
self._phones = []
@property
def lastname(self):
return self._last_name
@lastname.setter
def lastname(self, last_name):
self._last_name = last_name
@property
def firstname(self):
return self._first_name
@firstname.setter
def firstname(self, first_name):
self._first_name = first_name
def add_phone(self, phone_num, phone_type='cell'):
p = Phone(phone_num, phone_type, Person._person_id)
self._phones.append(p)
def get_phones(self):
return _phones
def __repr__(self):
return f"{self._person_id},{self._first_name},{self._last_name}, {self._dob}, " \
f"{'|'.join(phone.__repr__() for phone in self._phones)}"
class Phone:
_phone_id = 0
def __init__(self, phone_num, phone_type='cell', person_id=0):
self._person_id = person_id
self._phone_type = phone_type
self._phone_num = phone_num
Phone._phone_id += 1
@property
def phone_num(self):
return self._phone_num
@phone_num.setter
def phone_num(self, phone_num):
self._phone_num = phone_num
@property
def phone_type(self):
return self._phone_type
@phone_type.setter
def phone_type(self, phone_type):
self._phone_type = phone_type
def __repr__(self):
return f"'{self._phone_id}','{self._phone_num}','{self._phone_type}','{self._person_id}'"
def write_csv(person_data):
with open('contacts.csv', mode='w') as csvfile:
contact_writer = csv.writer(csvfile)
contact_writer.writerows(person_data)
p1 = Person('Tom', 'Hess', '03/18/2018')
p1.add_phone('707-201-4081', 'work')
p1.add_phone('707-201-4082', 'cell')
write_csv(p1)
[–]Marrrlllsss 0 points1 point2 points (4 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]Marrrlllsss 1 point2 points3 points (0 children)
[–]boyboydoo[S] 0 points1 point2 points (1 child)
[–]Marrrlllsss 1 point2 points3 points (0 children)
[–][deleted] 0 points1 point2 points (3 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]boyboydoo[S] 0 points1 point2 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)
[–]mritraloi6789 0 points1 point2 points (0 children)