Re: newbie question: if var1 == var2:
On Thu, 2008-12-11 at 13:44 -0600, Kirk Strauser wrote: At 2008-12-11T17:24:44Z, rdmur...@bitdance.com writes: ' ab c \r\n'.rstrip('\r\n') ' ab c ' ' ab c \n'.rstrip('\r\n') ' ab c ' ' ab c '.rstrip('\r\n') ' ab c ' I didn't say it couldn't be done. I just like the Perl version better. Python has a version equally good: def chomp(s): return s.rstrip('\r\n') chomp(foo\n) chomp(foo) chomp(perl\r\n) You'll hardly miss Perl at all. ;) -- http://mail.python.org/mailman/listinfo/python-list
Re: newbie question: if var1 == var2:
At 2008-12-12T15:35:11Z, J. Cliff Dyer j...@sdf.lonestar.org writes: Python has a version equally good: def chomp(s): return s.rstrip('\r\n') You'll hardly miss Perl at all. ;) I haven't missed Perl in years! I just wish there was a basestring.stripeol method because I seem to end up writing the inline version of chomp every time I iterate across a file. -- Kirk Strauser The Day Companies -- http://mail.python.org/mailman/listinfo/python-list
Re: newbie question: if var1 == var2:
Kirk Strauser wrote: At 2008-12-12T15:35:11Z, J. Cliff Dyer j...@sdf.lonestar.org writes: Python has a version equally good: def chomp(s): return s.rstrip('\r\n') You'll hardly miss Perl at all. ;) I haven't missed Perl in years! I just wish there was a basestring.stripeol method because I seem to end up writing the inline version of chomp every time I iterate across a file. Python is open source. You could suggest adding it and/or provide a patch. -- http://mail.python.org/mailman/listinfo/python-list
Re: newbie question: if var1 == var2:
Andrew Robert wrote: Two issues regarding script. You have a typo on the file you are trying to open. It is listed with a file extension of .in when it should be .ini . Pardon? The OPs original post used .in both in the python code and the command line. Doesn't look like a typo to me. Out of curiosity, what types of .ini files have one text string per line without = ? The ones I have seen follow this format: [section name] setting1 = a value setting2 = another value ~Ethan~ -- http://mail.python.org/mailman/listinfo/python-list
Re: newbie question: if var1 == var2:
At 2008-11-29T04:02:11Z, Mel [EMAIL PROTECTED] writes: You could try for item in fname: item = item.strip() This is one case where I really miss Perl's chomp function. It removes a trailing newline and nothing else, so you don't have to worry about losing leading or trailing spaces if those are important to you. -- Kirk Strauser The Day Companies -- http://mail.python.org/mailman/listinfo/python-list
Re: newbie question: if var1 == var2:
On Thu, 11 Dec 2008 at 10:24, Kirk Strauser wrote: At 2008-11-29T04:02:11Z, Mel [EMAIL PROTECTED] writes: You could try for item in fname: item = item.strip() This is one case where I really miss Perl's chomp function. It removes a trailing newline and nothing else, so you don't have to worry about losing leading or trailing spaces if those are important to you. ' ab c \r\n'.rstrip('\r\n') ' ab c ' ' ab c \n'.rstrip('\r\n') ' ab c ' ' ab c '.rstrip('\r\n') ' ab c ' --RDM -- http://mail.python.org/mailman/listinfo/python-list
Re: newbie question: if var1 == var2:
Kirk Strauser wrote: At 2008-11-29T04:02:11Z, Mel mwil...@the-wire.com writes: You could try for item in fname: item = item.strip() This is one case where I really miss Perl's chomp function. It removes a trailing newline and nothing else, so you don't have to worry about losing leading or trailing spaces if those are important to you. ... and it's so hard to write item = item[:-1] regards Steve -- Steve Holden+1 571 484 6266 +1 800 494 3119 Holden Web LLC http://www.holdenweb.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: newbie question: if var1 == var2:
At 2008-12-11T17:24:44Z, rdmur...@bitdance.com writes: ' ab c \r\n'.rstrip('\r\n') ' ab c ' ' ab c \n'.rstrip('\r\n') ' ab c ' ' ab c '.rstrip('\r\n') ' ab c ' I didn't say it couldn't be done. I just like the Perl version better. -- Kirk Strauser The Day Companies -- http://mail.python.org/mailman/listinfo/python-list
Re: newbie question: if var1 == var2:
On Thu, 11 Dec 2008 13:44:22 -0600, Kirk Strauser wrote: At 2008-12-11T17:24:44Z, rdmur...@bitdance.com writes: ' ab c \r\n'.rstrip('\r\n') ' ab c ' ' ab c \n'.rstrip('\r\n') ' ab c ' ' ab c '.rstrip('\r\n') ' ab c ' I didn't say it couldn't be done. I just like the Perl version better. def chomp(s): return s.rstrip('\r\n') And now you have chomp. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: newbie question: if var1 == var2:
Steve Holden a écrit : Kirk Strauser wrote: At 2008-11-29T04:02:11Z, Mel mwil...@the-wire.com writes: You could try for item in fname: item = item.strip() This is one case where I really miss Perl's chomp function. It removes a trailing newline and nothing else, so you don't have to worry about losing leading or trailing spaces if those are important to you. ... and it's so hard to write item = item[:-1] Steve, you should know better. It's item = item.rstrip('\r\n') -- http://mail.python.org/mailman/listinfo/python-list
Re: newbie question: if var1 == var2:
At 2008-12-11T19:49:23Z, Steve Holden st...@holdenweb.com writes: ... and it's so hard to write item = item[:-1] It's easy - and broken. Bad things happen if you're using something other than '\n' for EOL. -- Kirk Strauser The Day Companies -- http://mail.python.org/mailman/listinfo/python-list
Re: newbie question: if var1 == var2:
On Thu, 11 Dec 2008 19:49:23 -, Steve Holden st...@holdenweb.com wrote: Kirk Strauser wrote: At 2008-11-29T04:02:11Z, Mel mwil...@the-wire.com writes: You could try for item in fname: item = item.strip() This is one case where I really miss Perl's chomp function. It removes a trailing newline and nothing else, so you don't have to worry about losing leading or trailing spaces if those are important to you. ... and it's so hard to write item = item[:-1] Tsk. That would be chop. chomp would be if item[-1] == '\n': item = item[:-1] :-) -- Rhodri James *-* Wildebeeste Herder to the Masses -- http://mail.python.org/mailman/listinfo/python-list
Re: newbie question: if var1 == var2:
Kirk Strauser wrote: At 2008-12-11T19:49:23Z, Steve Holden st...@holdenweb.com writes: item = item[:-1] It's easy - and broken. Bad things happen if you're using something other than '\n' for EOL. Or if the last line of your file doesn't end with a newline. -- Greg -- http://mail.python.org/mailman/listinfo/python-list
Re: newbie question: if var1 == var2:
On Dec 12, 10:31 am, Rhodri James rho...@wildebst.demon.co.uk wrote: On Thu, 11 Dec 2008 19:49:23 -, Steve Holden st...@holdenweb.com wrote: Kirk Strauser wrote: At 2008-11-29T04:02:11Z, Mel mwil...@the-wire.com writes: You could try for item in fname: item = item.strip() This is one case where I really miss Perl's chomp function. It removes a trailing newline and nothing else, so you don't have to worry about losing leading or trailing spaces if those are important to you. ... and it's so hard to write item = item[:-1] Tsk. That would be chop. chomp would be if item[-1] == '\n': item = item[:-1] Better: if item and item[-1] == '\n': return item[:-1] return item -- http://mail.python.org/mailman/listinfo/python-list
Re: newbie question: if var1 == var2:
On Dec 11, 3:49 pm, John Machin sjmac...@lexicon.net wrote: On Dec 12, 10:31 am, Rhodri James rho...@wildebst.demon.co.uk wrote: On Thu, 11 Dec 2008 19:49:23 -, Steve Holden st...@holdenweb.com wrote: Kirk Strauser wrote: At 2008-11-29T04:02:11Z, Mel mwil...@the-wire.com writes: You could try for item in fname: item = item.strip() This is one case where I really miss Perl's chomp function. It removes a trailing newline and nothing else, so you don't have to worry about losing leading or trailing spaces if those are important to you. ... and it's so hard to write item = item[:-1] Tsk. That would be chop. chomp would be if item[-1] == '\n': item = item[:-1] Better: if item and item[-1] == '\n': return item[:-1] return item Best: return item \ if not (item and item.endswith('\n')) \ else item[:-1] Though really you should be using item.rstrip() -- http://mail.python.org/mailman/listinfo/python-list
Re: newbie question: if var1 == var2:
Jason Scheirer wrote: On Dec 11, 3:49 pm, John Machin sjmac...@lexicon.net wrote: On Dec 12, 10:31 am, Rhodri James rho...@wildebst.demon.co.uk wrote: On Thu, 11 Dec 2008 19:49:23 -, Steve Holden st...@holdenweb.com wrote: Kirk Strauser wrote: At 2008-11-29T04:02:11Z, Mel mwil...@the-wire.com writes: You could try for item in fname: item = item.strip() This is one case where I really miss Perl's chomp function. It removes a trailing newline and nothing else, so you don't have to worry about losing leading or trailing spaces if those are important to you. ... and it's so hard to write item = item[:-1] Tsk. That would be chop. chomp would be if item[-1] == '\n': item = item[:-1] Better: if item and item[-1] == '\n': return item[:-1] return item Best: return item \ if not (item and item.endswith('\n')) \ else item[:-1] Though really you should be using item.rstrip() Why not just: item[:-1] if item.endswith('\n') else item -- http://mail.python.org/mailman/listinfo/python-list
Re: newbie question: if var1 == var2:
On Thu, 11 Dec 2008 23:49:10 -, John Machin sjmac...@lexicon.net wrote: On Dec 12, 10:31 am, Rhodri James rho...@wildebst.demon.co.uk wrote: On Thu, 11 Dec 2008 19:49:23 -, Steve Holden st...@holdenweb.com wrote: ... and it's so hard to write item = item[:-1] Tsk. That would be chop. chomp would be if item[-1] == '\n': item = item[:-1] Better: if item and item[-1] == '\n': return item[:-1] return item If we aren't going for rstrip, yes, but I was trying to stick to Steve's metier. -- Rhodri James *-* Wildebeeste Herder to the Masses -- http://mail.python.org/mailman/listinfo/python-list
Re: newbie question: if var1 == var2:
On Dec 12, 11:39 am, MRAB goo...@mrabarnett.plus.com wrote: Jason Scheirer wrote: On Dec 11, 3:49 pm, John Machin sjmac...@lexicon.net wrote: On Dec 12, 10:31 am, Rhodri James rho...@wildebst.demon.co.uk wrote: On Thu, 11 Dec 2008 19:49:23 -, Steve Holden st...@holdenweb.com wrote: Kirk Strauser wrote: At 2008-11-29T04:02:11Z, Mel mwil...@the-wire.com writes: You could try for item in fname: item = item.strip() This is one case where I really miss Perl's chomp function. It removes a trailing newline and nothing else, so you don't have to worry about losing leading or trailing spaces if those are important to you. ... and it's so hard to write item = item[:-1] Tsk. That would be chop. chomp would be if item[-1] == '\n': item = item[:-1] Better: if item and item[-1] == '\n': return item[:-1] return item Best: return item \ if not (item and item.endswith('\n')) \ else item[:-1] Though really you should be using item.rstrip() Why not just: item[:-1] if item.endswith('\n') else item Some possible reasons: * because you might be supporting old versions of Python (my offering runs on 1.5) * because the true_value if condition else false_value syntax gives you the screaming dry Edgar Britts * because you'd prefer not to have the overhead of a method lookup and method call -- http://mail.python.org/mailman/listinfo/python-list
Re: newbie question: if var1 == var2:
John Machin wrote: On Dec 12, 11:39 am, MRAB goo...@mrabarnett.plus.com wrote: Jason Scheirer wrote: On Dec 11, 3:49 pm, John Machin sjmac...@lexicon.net wrote: On Dec 12, 10:31 am, Rhodri James rho...@wildebst.demon.co.uk wrote: On Thu, 11 Dec 2008 19:49:23 -, Steve Holden st...@holdenweb.com wrote: Kirk Strauser wrote: At 2008-11-29T04:02:11Z, Mel mwil...@the-wire.com writes: You could try for item in fname: item = item.strip() This is one case where I really miss Perl's chomp function. It removes a trailing newline and nothing else, so you don't have to worry about losing leading or trailing spaces if those are important to you. ... and it's so hard to write item = item[:-1] Tsk. That would be chop. chomp would be if item[-1] == '\n': item = item[:-1] Better: if item and item[-1] == '\n': return item[:-1] return item Best: return item \ if not (item and item.endswith('\n')) \ else item[:-1] Though really you should be using item.rstrip() Why not just: item[:-1] if item.endswith('\n') else item Some possible reasons: * because you might be supporting old versions of Python (my offering runs on 1.5) * because the true_value if condition else false_value syntax gives you the screaming dry Edgar Britts * because you'd prefer not to have the overhead of a method lookup and method call OK: if item[-1:] == '\n': return item[:-1] return item -- http://mail.python.org/mailman/listinfo/python-list
Re: newbie question: if var1 == var2:
On Dec 12, 1:11 pm, MRAB goo...@mrabarnett.plus.com wrote: John Machin wrote: On Dec 12, 11:39 am, MRAB goo...@mrabarnett.plus.com wrote: Jason Scheirer wrote: On Dec 11, 3:49 pm, John Machin sjmac...@lexicon.net wrote: On Dec 12, 10:31 am, Rhodri James rho...@wildebst.demon.co.uk wrote: On Thu, 11 Dec 2008 19:49:23 -, Steve Holden st...@holdenweb.com wrote: Kirk Strauser wrote: At 2008-11-29T04:02:11Z, Mel mwil...@the-wire.com writes: You could try for item in fname: item = item.strip() This is one case where I really miss Perl's chomp function. It removes a trailing newline and nothing else, so you don't have to worry about losing leading or trailing spaces if those are important to you. ... and it's so hard to write item = item[:-1] Tsk. That would be chop. chomp would be if item[-1] == '\n': item = item[:-1] Better: if item and item[-1] == '\n': return item[:-1] return item Best: return item \ if not (item and item.endswith('\n')) \ else item[:-1] Though really you should be using item.rstrip() Why not just: item[:-1] if item.endswith('\n') else item Some possible reasons: * because you might be supporting old versions of Python (my offering runs on 1.5) * because the true_value if condition else false_value syntax gives you the screaming dry Edgar Britts * because you'd prefer not to have the overhead of a method lookup and method call OK: if item[-1:] == '\n': return item[:-1] return item I'll pay that :-) -- http://mail.python.org/mailman/listinfo/python-list
newbie question: if var1 == var2:
Hi All, I dont understand why the following code never finds tree. I could not find the answer in the Python tutorials. Here is the code, test43.in, and runtime: #!/usr/bin/python fname = open(test43.in) var = 'tree' for item in fname: print item: , item, if item == var: print found tree: , item, [EMAIL PROTECTED] work]$ [EMAIL PROTECTED] work]$ [EMAIL PROTECTED] work]$ cat test43.in car tree house pool dog cat wax candy bar [EMAIL PROTECTED] work]$ python test43.py item: car item: tree item: house item: pool item: dog item: cat item: wax item: candy bar Thanks, [EMAIL PROTECTED] -- http://mail.python.org/mailman/listinfo/python-announce-list Support the Python Software Foundation: http://www.python.org/psf/donations.html
Re: newbie question: if var1 == var2:
Any reason for posting such an issue to the account list? Pillock! On Sat, Nov 29, 2008 at 4:47 AM, [EMAIL PROTECTED] wrote: Hi All, I dont understand why the following code never finds tree. I could not find the answer in the Python tutorials. Here is the code, test43.in, and runtime: #!/usr/bin/python fname = open(test43.in) var = 'tree' for item in fname: print item: , item, if item == var: print found tree: , item, [EMAIL PROTECTED] work]$ [EMAIL PROTECTED] work]$ [EMAIL PROTECTED] work]$ cat test43.in car tree house pool dog cat wax candy bar [EMAIL PROTECTED] work]$ python test43.py item: car item: tree item: house item: pool item: dog item: cat item: wax item: candy bar Thanks, [EMAIL PROTECTED] -- http://mail.python.org/mailman/listinfo/python-announce-list Support the Python Software Foundation: http://www.python.org/psf/donations.html -- http://mail.python.org/mailman/listinfo/python-list
Re: newbie question: if var1 == var2:
item = tree\n != 'tree' [EMAIL PROTECTED] wrote: Hi All, I dont understand why the following code never finds tree. I could not find the answer in the Python tutorials. Here is the code, test43.in, and runtime: #!/usr/bin/python fname = open(test43.in) var = 'tree' for item in fname: print item: , item, if item == var: print found tree: , item, [EMAIL PROTECTED] work]$ [EMAIL PROTECTED] work]$ [EMAIL PROTECTED] work]$ cat test43.in car tree house pool dog cat wax candy bar [EMAIL PROTECTED] work]$ python test43.py item: car item: tree item: house item: pool item: dog item: cat item: wax item: candy bar Thanks, [EMAIL PROTECTED] -- http://mail.python.org/mailman/listinfo/python-announce-list Support the Python Software Foundation: http://www.python.org/psf/donations.html -- http://mail.python.org/mailman/listinfo/python-list
Re: newbie question: if var1 == var2:
It's the newline after each word that's messing you up. var = tree\n ... or if item.strip() == var: ... etc. Kirby On Fri, Nov 28, 2008 at 7:47 PM, [EMAIL PROTECTED] wrote: Hi All, I dont understand why the following code never finds tree. I could not find the answer in the Python tutorials. Here is the code, test43.in, and runtime: #!/usr/bin/python fname = open(test43.in) var = 'tree' for item in fname: print item: , item, if item == var: print found tree: , item, [EMAIL PROTECTED] work]$ [EMAIL PROTECTED] work]$ [EMAIL PROTECTED] work]$ cat test43.in car tree house pool dog cat wax candy bar [EMAIL PROTECTED] work]$ python test43.py item: car item: tree item: house item: pool item: dog item: cat item: wax item: candy bar Thanks, [EMAIL PROTECTED] -- http://mail.python.org/mailman/listinfo/python-announce-list Support the Python Software Foundation: http://www.python.org/psf/donations.html -- http://mail.python.org/mailman/listinfo/python-list
Re: newbie question: if var1 == var2:
[EMAIL PROTECTED] wrote: Hi All, I dont understand why the following code never finds tree. I could not find the answer in the Python tutorials. Here is the code, test43.in, and runtime: #!/usr/bin/python fname = open(test43.in) var = 'tree' for item in fname: print item: , item, if item == var: print found tree: , item, [EMAIL PROTECTED] work]$ [EMAIL PROTECTED] work]$ [EMAIL PROTECTED] work]$ cat test43.in car tree house pool dog cat wax candy bar [EMAIL PROTECTED] work]$ python test43.py item: car item: tree item: house item: pool item: dog item: cat item: wax item: candy bar Thanks, [EMAIL PROTECTED] Here's how you debug it: fname = open(test43.in) var = 'tree' for item in fname: print item: %r % item, # Was: print item: , item, if item == var: print found tree: , item, -- http://mail.python.org/mailman/listinfo/python-list
Re: newbie question: if var1 == var2:
On Fri, 2008-11-28 at 19:47 -0800, [EMAIL PROTECTED] wrote: Hi All, I dont understand why the following code never finds tree. The problem is that the lines you are reading from the file have a newline at the end so 'tree' != 'tree\n'. See below for suggested changes. I could not find the answer in the Python tutorials. Here is the code, test43.in, and runtime: #!/usr/bin/python fp = open(test43.in) var = 'tree' for item in fp: item = item.strip() # Strip off white space # No commas necessary at the end of the prints # now that we've removed the newlines. print item:, item if item == var: print found tree:, item Hope this helps you understand what was happening. Have fun with python! -Reggie -- http://mail.python.org/mailman/listinfo/python-list
Re: newbie question: if var1 == var2:
because when you loop over open(...) is the same as looping over open (...).readlines() and readlines() reads everything including newlines. Try replace: if item == var: with if item.strip() == var: Massimo On Nov 28, 2008, at 9:47 PM, [EMAIL PROTECTED] wrote: Hi All, I dont understand why the following code never finds tree. I could not find the answer in the Python tutorials. Here is the code, test43.in, and runtime: #!/usr/bin/python fname = open(test43.in) var = 'tree' for item in fname: print item: , item, if item == var: print found tree: , item, [EMAIL PROTECTED] work]$ [EMAIL PROTECTED] work]$ [EMAIL PROTECTED] work]$ cat test43.in car tree house pool dog cat wax candy bar [EMAIL PROTECTED] work]$ python test43.py item: car item: tree item: house item: pool item: dog item: cat item: wax item: candy bar Thanks, [EMAIL PROTECTED] -- http://mail.python.org/mailman/listinfo/python-announce-list Support the Python Software Foundation: http://www.python.org/psf/donations.html -- http://mail.python.org/mailman/listinfo/python-list
Re: newbie question: if var1 == var2:
On Nov 29, 2:53 pm, [EMAIL PROTECTED] wrote: Hi All, I dont understand why the following code cannot find the variable tree. It is very simple but I could not find the answer to this on the Python Tutorials. Here is the code, input and runtime: #!/usr/bin/python fname = open(test43.in) var = 'tree' for item in fname: print item: , item, The repr() built-in function is your friend. Having trouble with item == var? Do this: print repr(var), repr(item) and you'll see immediately why item != var if item == var: print found tree: , item, HTH, John -- http://mail.python.org/mailman/listinfo/python-list
Re: newbie question: if var1 == var2:
Two issues regarding script. You have a typo on the file you are trying to open. It is listed with a file extension of .in when it should be .ini . The next issue is that you are comparing what was read from the file versus the variable. The item read from file also contains and end-of-line character so they will never match. To get around this: #!/usr/bin/python fname = open("test43.ini") var = 'tree' for item in fname: print "item: ", item, if (item.rstrip("\n") == var): print "found tree: ", item, else: print "No tree found" David wrote: Il Fri, 28 Nov 2008 19:47:01 -0800 (PST), [EMAIL PROTECTED] ha scritto: I dont understand why the following code never finds "tree". New line marker to be stripped? if item == var: if item.strip() == var: D. -- http://mail.python.org/mailman/listinfo/python-announce-list Support the Python Software Foundation: http://www.python.org/psf/donations.html -- http://mail.python.org/mailman/listinfo/python-list
newbie question: if var1 == var2:
Hi All, I dont understand why the following code cannot find the variable tree. It is very simple but I could not find the answer to this on the Python Tutorials. Here is the code, input and runtime: #!/usr/bin/python fname = open(test43.in) var = 'tree' for item in fname: print item: , item, if item == var: print found tree: , item, [EMAIL PROTECTED] work]$ [EMAIL PROTECTED] work]$ [EMAIL PROTECTED] work]$ cat test43.in car tree house pool dog cat wax candy bar [EMAIL PROTECTED] work]$ python test43.py item: car item: tree item: house item: pool item: dog item: cat item: wax item: candy bar [EMAIL PROTECTED] work]$ Thanks for the help, [EMAIL PROTECTED] -- http://mail.python.org/mailman/listinfo/python-list
Re: newbie question: if var1 == var2:
[EMAIL PROTECTED] wrote: Hi All, I dont understand why the following code cannot find the variable tree. It is very simple but I could not find the answer to this on the Python Tutorials. Here is the code, input and runtime: #!/usr/bin/python fname = open(test43.in) var = 'tree' for item in fname: print item: , item, if item == var: print found tree: , item, Because each item from the file has a newline character at the end. Notice how your print statements end with ','. This suppresses the print statement's newline, and the one on the end of the item makes the printout look normal. You could try for item in fname: item = item.strip() # ... etc. Mel. -- http://mail.python.org/mailman/listinfo/python-list
Re: newbie question: if var1 == var2:
On Nov 29, 1:53 pm, [EMAIL PROTECTED] wrote: I dont understand why the following code cannot find the variable tree. fname = open(test43.in) var = 'tree' for item in fname: This will include the EOL character for each line. Try adding the following line here: item = item.strip() print item: , item, This would have been more obvious without the trailing ',' which suppresses a new-line being added when printing. if item == var: print found tree: , item, -- http://mail.python.org/mailman/listinfo/python-list