Skip to content
Advertisement

Formatting output of CSV file in Python

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:

JavaScript

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:

JavaScript

My current code is as follows:

JavaScript

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

JavaScript

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.

Advertisement

Answer

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

JavaScript

Output:

JavaScript

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.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement