2

I have text file like this:

A fistful of Dolars|Western|100|Sergio Leone|Clint Eastwood|Italia|1964
For a few dolars more|Western|130|Sergio Leone|Clint Eastwood|Italia|1965
The Good, the Bad and the Ugly|Western|179|Sergio Leone|Clint Eastwood|Italia|1966

And I try it to format on this way:

def movie_list():
    movies = open('movies.txt','r').readlines()
    for i in movies:
        movie = i.strip("\n").split("|")
        for args in (('Name','Genre','Running time', 'Director', 'Starring', 'Country', 'Released'),(movie[0], movie[1], movie[2], movie[3], movie[4], movie[5], movie[6]+"\n")):
            print (('{0:<13} {1:<10} {2:<10} {3:<13} {4:<13} {5:<8} {6:<4}').format(*args))

And on few similar ways...

How to set size of row depending of length of string for each line in text file (i.e. movie)

(I did it in paint :D) The best way is to look like this:

Community
  • 1
  • 1
Neo Cortex
  • 47
  • 1
  • 11
  • 1
    I am not sure what you are trying to do. Do you want the title column width to be the width of the longest title from any row? – James Dec 15 '17 at 01:15
  • Possible duplicate https://stackoverflow.com/a/3685943/8881141 or https://stackoverflow.com/a/9989441/8881141 – Mr. T Dec 15 '17 at 01:30
  • @Piinthesky I tried on this way stackoverflow.com/a/9989441/8881141 but then it's every row resized on size on longest string. – Neo Cortex Dec 15 '17 at 01:39
  • 1
    This one keeps track of the width for each column: https://stackoverflow.com/a/12065663/8881141 The best solution is not always the one with most votes. – Mr. T Dec 15 '17 at 01:44

1 Answers1

0

Following the methodology outlined in stackoverflow.com/a/12065663/8881141 works nicely:

movies = []

with open('movies.txt', 'r') as f:
    for line in f:
        movies.append(line.strip('\n').split('|'))

f.close()

widths = [max(map(len, col)) for col in zip(*movies)]

for movie in movies:
    print('   '.join([col.ljust(val) for col, val in zip(movie, widths)]))

It's worth noting that a monospace font is required to get the desired appearance.

Pat Jones
  • 876
  • 8
  • 18