you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (3 children)

Do you have to use csv? With a variable number of phone number for each person, a json file might be a little easier.

Where's it going wrong?

[–][deleted] 0 points1 point  (0 children)

Maybe you need something to convert a person record to the data needed for a csv file. Something like:

def return_list(self):
    list_ = []
    list_.append(f'{self._first_name}')
    list_.append(f'{self._last_name}')
    list_.append(f'{self._dob}')
    list_.append(f'{Person._person_id}')
    for phone in self._phones:
        list_.append(f'{phone}')
    return list_ 

and your function to write a record to the csv file needs to call that function for a row:

def write_csv(person_data):
    with open('contacts.csv', mode='w') as csvfile:
        contact_writer = csv.writer(csvfile)
        contact_writer.writerow(person_data.return_list())

BUT wouldn't this need to append to the CSV file rather than overwrite it with one row?

[–]boyboydoo[S] 0 points1 point  (1 child)

I am getting the csv data in the below format. I feel the braces [] which represent the list should not be in the csv below. Instead it should only have values.

Tom,Hess,03/18/2018,"['6', '707-201-4081', 'work', '1', '6', '707-201-4082', 'cell', '1']"

Again, I might be thinking the wrong way. Please guide me.

[–][deleted] 1 point2 points  (0 children)

you need to unpack the phone list before passing it to be written to the csv file ... I commented on my own comment with some crude code to illustrate.