12

I am creating a very rudimentary "Address Book" program in Python. I am grabbing contact data from a CSV file, the contents of which looks like the following example:

Name,Phone,Company,Email
Elon Musk,454-6723,SpaceX,emusk@spacex.com
Larry Page,853-0653,Google,lpage@gmail.com
Tim Cook,133-0419,Apple,tcook@apple.com
Steve Ballmer,456-7893,Developers!,sballmer@bluescreen.com

I am trying to format the output so that it looks cleaner and more readable, i.e. everything lined up in rows and columns, like this:

Name:        Phone:        Company:        Email:        
Elon Musk    454-6723      SpaceX          emusk@spacex.com

My current code is as follows:

f = open("contactlist.csv")
csv_f = csv.reader(f)
for row in csv_f:
    print(row)

Which naturally due to lack of formatting, produces this, which still looks very unclean.

['Name', 'Phone', 'Company', 'Email']
['Elon Musk', '454-6723', 'SpaceX', 'emusk@spacex.com']
['Larry Page', '853-0653', 'Google', 'lpage@gmail.com']
['Tim Cook', '133-0419', 'Apple', 'tcook@apple.com']
['Steve Ballmer', '456-7893', 'Developers!', 'sballmer@bluescreen.com']

Any tips on how to produce a cleaner output would be greatly appreciated, as I am beginner and I find all of this quite confusing. Many thanks in advance.

2 Answers 2

14

You could use format to left justify your output. For example,

f = open("contactlist.csv")
csv_f = csv.reader(f)
for row in csv_f:
    print('{:<15}  {:<15}  {:<20} {:<25}'.format(*row))

Output:

Name             Phone            Company              Email                    
Elon Musk        454-6723         SpaceX               emusk@spacex.com         
Larry Page       853-0653         Google               lpage@gmail.com          
Tim Cook         133-0419         Apple                tcook@apple.com          
Steve Ballmer    456-7893         Developers!          sballmer@bluescreen.com  

You can read more about format here. The < symbol left-aligns the text, and the number specifies the width of the string. Each {} can include a positional argument before the colon : - if they are omitted, the strings will appear in the order of the arguments in the unpacked list row.

3
  • @zarak I tried your solution, but I keep getting IndexError: tuple index out of range any ideas for a solution?
    – Vash
    Dec 10, 2017 at 8:35
  • Hi @Vash, Are you running the code on the example shown here?
    – zarak
    Dec 11, 2017 at 10:00
  • @zarak No, i tried running it on a bigger csv file.
    – Vash
    Dec 11, 2017 at 19:05
1

There are a few python modules for creating tables through plain text, e.g. tabulate and prettytable. With tabulate the printing becomes a one-liner:

from tabulate import tabulate
f = open("contactlist.csv")
csv_f = csv.reader(f)
print(tabulate(csv_f, headers='firstrow'))

The output looks like this:

Name           Phone     Company      Email
-------------  --------  -----------  -----------------------
Elon Musk      454-6723  SpaceX       emusk@spacex.com
Larry Page     853-0653  Google       lpage@gmail.com
Tim Cook       133-0419  Apple        tcook@apple.com
Steve Ballmer  456-7893  Developers!  sballmer@bluescreen.com

You can also specify among a bunch of formatting options with the tablefmt keyword. The output of print(tabulate(csv_f,headers='firstrow', tablefmt='pipe')) for example corresponds to markdown table format:

| Name          | Phone    | Company     | Email                   |
|---------------|----------|-------------|-------------------------|
| Elon Musk     | 454-6723 | SpaceX      | emusk@spacex.com        |
| Larry Page    | 853-0653 | Google      | lpage@gmail.com         |
| Tim Cook      | 133-0419 | Apple       | tcook@apple.com         |
| Steve Ballmer | 456-7893 | Developers! | sballmer@bluescreen.com |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.