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

Sort the contacts using cool python tuples #4

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## [0.1.0] - 2017-02-20
### Added
* Add contact sorter code
15 changes: 14 additions & 1 deletion phonebook.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
#Import Python modules for handling csv
import sys
import csv
import os

#Open phonebook.csv file
myfile = csv.reader(open("phonebook.csv", 'rU'), delimiter=",", dialect=csv.excel_tab)

#Return information from file
contacts = []
header = next(myfile)
for first_name, last_name, phone1 in myfile:
print first_name
contacts.append((first_name, last_name, phone1))

sorted_by_last = sorted(contacts, key=lambda tup: tup[1])

print sorted_by_last

with open('output.csv','w') as out:
csv_out=csv.writer(out)
csv_out.writerow(header)
for row in sorted_by_last:
csv_out.writerow(row)