slicing the end of a string in a list

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


Ben Cartwright wrote:

>   print [line[:-1] for line in open('C:\\switches.txt')]

Hmm, I just realized in my original code that I didn't escape the 
backslash. Why did it still work properly?

By the way, this whole 'one line' thing has blown me away. I wasn't 
thinking about list comprehensions when I started working on this, but 
just the fact that it can all be done in one line is amazing. I tried 
this in C# and of course I had to create a class first, and open the 
file streams, etc.  :)

And do I not need the 'r' parameter in the open function?



More information about the Python-list mailing list