I have data (numbers) saved in the following format (example):
234 127 34 23 45567
23 12 4 4 45
23456 2 1 444 567
...
Is there any python-way method to line up the numbers and get them as
234 127 34 23 45567
23 12 4 4 45
23456 2 1 444 567
(I cannot predict the column size).
Aligning the output neatly using f-prefix You can use the :> , :< or :^ option in the f-format to left align, right align or center align the text that you want to format. We can use the fortmat() string function in python to output the desired text in the order we want.
We will take outer for loop, which is for how many lines we want to print. Since we want to print natural number column by column, for this we will take an inner for loop. Using the logic k = k + n – j, we will get natural numbers according to requirement.
Here is a simple, self-contained example that shows how to format variable column widths:
data = '''\
234 127 34 23 45567
23 12 4 4 45
23456 2 1 444 567'''
# Split input data by row and then on spaces
rows = [ line.strip().split(' ') for line in data.split('\n') ]
# Reorganize data by columns
cols = zip(*rows)
# Compute column widths by taking maximum length of values per column
col_widths = [ max(len(value) for value in col) for col in cols ]
# Create a suitable format string
format = ' '.join(['%%%ds' % width for width in col_widths ])
# Print each row using the computed format
for row in rows:
print format % tuple(row)
which outputs:
234 127 34 23 45567 23 12 4 4 45 23456 2 1 444 567
You need some way of finding the column size, maybe by reading all the data and finding the maximum width.
>>> line='234 127 34 23 45567'
>>> line.split()
['234', '127', '34', '23', '45567']
>>> max(map(len, line.split()))
5
Repeat over all lines, to find column size (e.g., 5).
Constructing a formatted line with percent formatting
is straightforward.
>>> colsize = 5
>>> ' '.join(('%*s' % (colsize, i) for i in line.split()))
' 234 127 34 23 45567'
>>>
#!/usr/bin/env python
class ALIGN:
LEFT, RIGHT = '-', ''
class Column(list):
def __init__(self, name, data, align=ALIGN.RIGHT):
list.__init__(self, data)
self.name = name
width = max(len(str(x)) for x in data + [name])
self.format = ' %%%s%ds ' % (align, width)
class Table:
def __init__(self, *columns):
self.columns = columns
self.length = max(len(x) for x in columns)
def get_row(self, i=None):
for x in self.columns:
if i is None:
yield x.format % x.name
else:
yield x.format % x[i]
def get_rows(self):
yield ' '.join(self.get_row(None))
for i in range(0, self.length):
yield ' '.join(self.get_row(i))
def __str__(self):
return '\n'.join(self.get_rows())
For your example:
if __name__ == '__main__':
print Table(
Column("", [234, 32, 23456]),
Column("", [127, 12, 2]),
Column("", [34, 4, 1]),
Column("", [23, 4, 444]),
Column("", [45567, 45, 567])
)
It will yield:
234 127 34 23 45567
32 12 4 4 45
23456 2 1 444 567
Adapted from http://code.activestate.com/recipes/577202-render-tables-for-text-interface/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With