[Tutor] Formatting output into columns
Luke Paireepinart
rabidpoobear at gmail.com
Fri Aug 31 05:03:48 CEST 2007
Scott Oertel wrote:
> Someone asked me this question the other day, and I couldn't think of
> any easy way of printing the output besides what I came up with pasted
> below.
>
> So what you have is a file with words in it as such:
>
> apple
> john
> bean
> joke
> ample
> python
> nice
>
> and you want to sort and output the text into columns as such:
>
> a p j b n
> apple python john bean nice
> ample joke
>
> and this is what works, but I would also like to know how to wrap the
> columns, plus any ideas on a better way to accomplish this.
>
> #!/usr/bin/env python
>
> data = {}
> lrgColumn = 0
>
> for line in open("test.txt","r").read().splitlines():
>
you can just directly do
for line in open('test.txt'):
depending on your Python version. I believe it's 2.3+.
I have 2.4 and it works there, at least.
If using an older version of Python, you can use .readlines() instead of
.read().splitlines()
I believe Kent and Alan already helped you with your original question.
-Luke
More information about the Tutor
mailing list