newbie question: if var1 == var2:

J

joemacbusiness

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,
[jmccaughan@dhcppc2 work]$
[jmccaughan@dhcppc2 work]$
[jmccaughan@dhcppc2 work]$ cat test43.in
car
tree
house
pool
dog
cat
wax
candy bar
[jmccaughan@dhcppc2 work]$ python test43.py
item: car
item: tree
item: house
item: pool
item: dog
item: cat
item: wax
item: candy bar
[jmccaughan@dhcppc2 work]$

Thanks for the help, (e-mail address removed)
 
M

Mel

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.
 
A

alex23

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.
 
J

John Machin

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
 
R

rdmurray

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 '

--RDM
 
S

Steve Holden

Kirk said:
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
 
B

Bruno Desthuilliers

Steve Holden a écrit :
Kirk said:
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')
 
R

Rhodri James

Kirk said:
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]

:)
 
J

Jason Scheirer

Kirk Strauser wrote:
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()
 
M

MRAB

Jason said:
Kirk Strauser wrote:
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
 
J

John Machin

Jason said:
On Dec 12, 10:31 am, "Rhodri James" <[email protected]>
wrote:
Kirk Strauser wrote:
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

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
 
M

MRAB

John said:
Jason said:
On Dec 12, 10:31 am, "Rhodri James" <[email protected]>
wrote:
Kirk Strauser wrote:
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
 
J

John Machin

John said:
Jason Scheirer wrote:
On Dec 12, 10:31 am, "Rhodri James" <[email protected]>
wrote:
Kirk Strauser wrote:
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 :)
 
M

MRAB

Kirk said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top