python how to align text writen to a file 1

python how to align text writen to a file

file = open(file, "a") 
table_data = [
    ['a', 'b', 'c'],
    ['aaaaaaaaaa', 'b', 'c'], 
    ['a', 'bbbbbbbbbb', 'c']
]
for row in table_data:
    file.write("{: >20} {: >20} {: >20}".format(*row))

Here is what the above code is Doing:
1. Open the file in append mode.
2. Create a list of lists.
3. Loop through the list of lists.
4. Write the data to the file.

The first line of the file will look like this:
a b c

The second line of the file will look like this:
aaaaaaaaaa b c

The third line of the file will look like this:
a bbbbbbbbbb c

Similar Posts