Luke Paireepinart wrote:
> 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

The reason I use read().splitlines, is because if you do .readlines() it
adds the carriage return to the end of each line where in i have to
.rstrip() to remove it. If you use .read() it doesn't split the lines in
the file into a tuple, there for you it is not an iteration.


-Scott Oertel
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to