Most of the solutions here are just reformatting the data in the file that you are reading. Maybe that is all that you want.
If you actually want to parse the data, put it in a data structure.
This example in Python:
data="""\
Name: John Doe2
address : 123 Main St, Los Angeles, CA 95002
phone: 213-123-1234
Name: John Doe1
address : 145 Pearl St, La Jolla, CA 92013
phone: 858-123-1233
Name: Billy Bob Doe3
address : 454 Heartland St, Mobile, AL 00103
phone: 205-123-1232""".split('\n\n') # just a fill-in for your file
# you would use `with open(file) as data:`
addr={}
w0,w1,w2=0,0,0 # these keep track of the max width of the field
for line in data:
fields=[e.split(':')[1].strip() for e in [f for f in line.split('\n')]]
nam=fields[0].split()
name=nam[-1]+', '+' '.join(nam[0:-1])
addr[(name,fields[2])]=fields
w0,w1,w2=[max(t) for t in zip(map(len,fields),(w0,w1,w2))]
Now you have the freedom to sort, change the format, put in database, etc.
This prints your format with that data, sorted:
for add in sorted(addr.keys()):
print 'Name: {0:{w0}} Address: {1:{w1}} phone: {2:{w2}}'.format(*addr[add],w0=w0,w1=w1,w2=w2)
Prints:
Name: John Doe1 Address: 145 Pearl St, La Jolla, CA 92013 phone: 858-123-1233
Name: John Doe2 Address: 123 Main St, Los Angeles, CA 95002 phone: 213-123-1234
Name: Billy Bob Doe3 Address: 454 Heartland St, Mobile, AL 00103 phone: 205-123-1232
That is sorted by the last name, first name used in the dict key.
Now print it sorted by area code:
for add in sorted(addr.keys(),key=lambda x: addr[x][2] ):
print 'Name: {0:{w0}} Address: {1:{w1}} phone: {2:{w2}}'.format(*addr[add],w0=w0,w1=w1,w2=w2)
Prints:
Name: Billy Bob Doe3 Address: 454 Heartland St, Mobile, AL 00103 phone: 205-123-1232
Name: John Doe2 Address: 123 Main St, Los Angeles, CA 95002 phone: 213-123-1234
Name: John Doe1 Address: 145 Pearl St, La Jolla, CA 92013 phone: 858-123-1233
But, since you have the data in a indexed dictionary, you can print it as a table instead sorted by zip code:
# print table header
print '|{0:^{w0}}|{1:^{w1}}|{2:^{w2}}|'.format('Name','Address','Phone',w0=w0+2,w1=w1+2,w2=w2+2)
print '|{0:^{w0}}|{1:^{w1}}|{2:^{w2}}|'.format('----','-------','-----',w0=w0+2,w1=w1+2,w2=w2+2)
# print data sorted by last field of the address - probably a zip code
for add in sorted(addr.keys(),key=lambda x: addr[x][1].split()[-1]):
print '|{0:>{w0}}|{1:>{w1}}|{2:>{w2}}|'.format(*addr[add],w0=w0+2,w1=w1+2,w2=w2+2)
Prints:
| Name | Address | Phone |
| ---- | ------- | ----- |
| Billy Bob Doe3| 454 Heartland St, Mobile, AL 00103| 205-123-1232|
| John Doe1| 145 Pearl St, La Jolla, CA 92013| 858-123-1233|
| John Doe2| 123 Main St, Los Angeles, CA 95002| 213-123-1234|