slicing the end of a string in a list

John Salerno johnjsal at NOSPAMgmail.com
Thu Mar 2 23:59:17 EST 2006


John Salerno wrote:
> Paul Rubin wrote:
> 
>> The preferred way to remove the newline is more like:
>>    for line in open('C:\\switches.txt'):
>>      print line.rstrip()
> 
> Interesting. So I would say:
> 
> [line.rstrip() for line in open('C:\\switches.txt')]

That seems to work. And on a related note, it seems to allow me to end 
my file on the last line, instead of having to add a newline character 
at the end of it so it will get sliced properly too.



More information about the Python-list mailing list