user2375231
user2375231

Reputation: 1

Python 3 - alignment and spacing

i am trying to create a new text file using information from another text file. i.e.

john doe, 88, uk
mike green, 212, usa

I want it to look like :

name                         number                               country

(left justified and specific width)

i am trying to align it left and create specific spacing between the columns.

i have serach this and cannot find a solution, the closest syntax is below:

lines = old_file.readlines()
print ("{0:<25} {1:<6} {2:<35}".format(*lines)

the above code does not work and I am not sure how to refer to the values from another text file - after the .format syntax.

Upvotes: 0

Views: 118

Answers (1)

Nicolas
Nicolas

Reputation: 5668

You forgot to split the lines and to loop over each one:

lines = old_file.readlines()
for l in lines:
    print('{0:<25} {1:<6} {2:<35}'.format(*l.split(',')))

 

john doe                   88     uk                                
mike green                 212    usa   

Upvotes: 2

Related Questions