Re: slicing the end of a string in a list

2006-03-06 Thread Paul Rubin
Steve Holden <[EMAIL PROTECTED]> writes: > >>>[line.rstrip() for line in open('C:\\switches.txt')] > > How would I manually close a file that's been opened this way? Or is > > it not possible in this case? Is it necessary? > > In CPython it's not strictly necessary to close the file, but other > im

Re: slicing the end of a string in a list

2006-03-06 Thread John Salerno
Steve Holden wrote: > It's not possible to perform an explicit close if, as in this case, you > don't have an explicit reference to the file object. > > In CPython it's not strictly necessary to close the file, but other > implementations don't guarantee that a file will be closed after the >

Re: slicing the end of a string in a list

2006-03-06 Thread Steve Holden
John Salerno wrote: > Paul Rubin wrote: > >>John Salerno <[EMAIL PROTECTED]> writes: >> >>>Interesting. So I would say: >>> >>>[line.rstrip() for line in open('C:\\switches.txt')] > > > > How would I manually close a file that's been opened this way? Or is it > not possible in this case? Is it

Re: slicing the end of a string in a list

2006-03-06 Thread John Salerno
Paul Rubin wrote: > John Salerno <[EMAIL PROTECTED]> writes: >> Interesting. So I would say: >> >> [line.rstrip() for line in open('C:\\switches.txt')] How would I manually close a file that's been opened this way? Or is it not possible in this case? Is it necessary? -- http://mail.python.org/m

Re: slicing the end of a string in a list

2006-03-03 Thread John Salerno
Steven D'Aprano wrote: > On Fri, 03 Mar 2006 16:57:10 +, John Salerno wrote: > >> Steven D'Aprano wrote: >> >>> The important term there is BINARY, not large. Many problems *reading* >>> (not opening) binary files will go away if you use 'rb', regardless of >>> whether they are small, medium o

Re: slicing the end of a string in a list

2006-03-03 Thread Steven D'Aprano
On Fri, 03 Mar 2006 16:57:10 +, John Salerno wrote: > Steven D'Aprano wrote: > >> The important term there is BINARY, not large. Many problems *reading* >> (not opening) binary files will go away if you use 'rb', regardless of >> whether they are small, medium or large. > > Is 'b' the proper

Re: slicing the end of a string in a list

2006-03-03 Thread John Salerno
Steven D'Aprano wrote: > The important term there is BINARY, not large. Many problems *reading* > (not opening) binary files will go away if you use 'rb', regardless of > whether they are small, medium or large. Is 'b' the proper parameter to use when you want to read/write a binary file? I was

Re: slicing the end of a string in a list

2006-03-03 Thread Steven D'Aprano
On Fri, 03 Mar 2006 01:03:38 -0800, P Boy wrote: > I had some issues while ago trying to open a large binary file. The important term there is BINARY, not large. Many problems *reading* (not opening) binary files will go away if you use 'rb', regardless of whether they are small, medium or large.

Re: slicing the end of a string in a list

2006-03-03 Thread P Boy
I had some issues while ago trying to open a large binary file. Anyway, from file() man page: If mode is omitted, it defaults to 'r'. When opening a binary file, you should append 'b' to the mode value for improved portability. (It's useful even on systems which don't treat binary and text files

Re: slicing the end of a string in a list

2006-03-03 Thread Peter Otten
P Boy wrote: > BTW, if the file is huge, one may want to consider using > open('c:\\switches.txt', 'rb') instead. Why? -- http://mail.python.org/mailman/listinfo/python-list

Re: slicing the end of a string in a list

2006-03-03 Thread P Boy
One liners are cool. Personally however, I would not promote one liners in Python. Python code is meant to be read. Cryptic coding is in perl's world. Code below is intuitive and almost a three year old would understand. for line in open('C:\\switches.txt'): print line.rstrip() BTW, if t

Re: slicing the end of a string in a list

2006-03-02 Thread Peter Otten
John Salerno wrote: > You can probably tell what I'm doing. Read a list of lines from a file, > and then I want to slice off the '\n' character from each line. If you are not concerned about memory consumption there is also open(filename).read().splitlines() Peter -- http://mail.python.org/mai

Re: slicing the end of a string in a list

2006-03-02 Thread Leif K-Brooks
Ben Cartwright wrote: > No, since the line variable is unused. This: > > i = 0 > for line in switches: > line = switches[i][:-1] > i += 1 > > Would be better written as: > > for i in range(len(switches)): > switches[i] = switches[i][:-1] This is better, IMHO: for i, sw

Re: slicing the end of a string in a list

2006-03-02 Thread Paul Rubin
John Salerno <[EMAIL PROTECTED]> writes: > Interesting. So I would say: > > [line.rstrip() for line in open('C:\\switches.txt')] Yes, you could do that. Note that it builds up an in-memory list of all those lines, instead of processing the file one line at a time. If the file is very large, that

Re: slicing the end of a string in a list

2006-03-02 Thread John Salerno
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 relat

Re: slicing the end of a string in a list

2006-03-02 Thread John Salerno
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')] -- http://mail.python.org/mailman/listinfo/python-list

Re: slicing the end of a string in a list

2006-03-02 Thread Paul Rubin
John Salerno <[EMAIL PROTECTED]> writes: > > 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? The character the backslash isn't special: \s doesn't get into a code like \n, s

Re: slicing the end of a string in a list

2006-03-02 Thread John Salerno
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 w

Re: slicing the end of a string in a list

2006-03-02 Thread John Salerno
Ben Cartwright wrote: > Actually, it creates a new string instance with the \n character > removed, then discards it. The original switches[0] string hasn't > changed. > Yes. You are repeated assigning a new string instance to "line", which > is then never referenced again. Ah, thank you! >

Re: slicing the end of a string in a list

2006-03-02 Thread Ben Cartwright
John Salerno wrote: > You can probably tell what I'm doing. Read a list of lines from a file, > and then I want to slice off the '\n' character from each line. But > after this code runs, the \n is still there. I thought it might have > something to do with the fact that strings are immutable, but

slicing the end of a string in a list

2006-03-02 Thread John Salerno
Here's the code I wrote: file = open('C:\switches.txt', 'r') switches = file.readlines() i = 0 for line in switches: line = switches[i][:-1] i += 1 print switches You can probably tell what I'm doing. Read a list of lines from a file, and then I want to slice off the '