newbie question: if var1 == var2:

Discussion in 'Python' started by joemacbusiness@gmail.com, Nov 29, 2008.

  1. Guest

    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,
     
    , Nov 29, 2008
    #1
    1. Advertising

  2. Mel Guest

    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.
     
    Mel, Nov 29, 2008
    #2
    1. Advertising

  3. alex23 Guest

    On Nov 29, 1:53 pm, 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,
     
    alex23, Nov 29, 2008
    #3
  4. John Machin Guest

    On Nov 29, 2:53 pm, 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
     
    John Machin, Nov 29, 2008
    #4
  5. Guest

    On Thu, 11 Dec 2008 at 10:24, Kirk Strauser wrote:
    > At 2008-11-29T04:02:11Z, Mel <> 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
     
    , Dec 11, 2008
    #5
  6. Steve Holden Guest

    Kirk Strauser wrote:
    > At 2008-11-29T04:02:11Z, Mel <> 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/
     
    Steve Holden, Dec 11, 2008
    #6
  7. Steve Holden a écrit :
    > Kirk Strauser wrote:
    >> At 2008-11-29T04:02:11Z, Mel <> 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')
     
    Bruno Desthuilliers, Dec 11, 2008
    #7
  8. On Thu, 11 Dec 2008 13:44:22 -0600, Kirk Strauser wrote:

    > At 2008-12-11T17:24:44Z, 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
     
    Steven D'Aprano, Dec 11, 2008
    #8
  9. Rhodri James Guest

    On Thu, 11 Dec 2008 19:49:23 -0000, Steve Holden <>
    wrote:

    > Kirk Strauser wrote:
    >> At 2008-11-29T04:02:11Z, Mel <> 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
     
    Rhodri James, Dec 11, 2008
    #9
  10. John Machin Guest

    On Dec 12, 10:31 am, "Rhodri James" <>
    wrote:
    > On Thu, 11 Dec 2008 19:49:23 -0000, Steve Holden <>  
    > wrote:
    >
    >
    >
    > > Kirk Strauser wrote:
    > >> At 2008-11-29T04:02:11Z, Mel <> 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
     
    John Machin, Dec 11, 2008
    #10
  11. greg Guest

    Kirk Strauser wrote:
    > At 2008-12-11T19:49:23Z, Steve Holden <> 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
     
    greg, Dec 11, 2008
    #11
  12. On Dec 11, 3:49 pm, John Machin <> wrote:
    > On Dec 12, 10:31 am, "Rhodri James" <>
    > wrote:
    >
    >
    >
    > > On Thu, 11 Dec 2008 19:49:23 -0000, Steve Holden <>  
    > > wrote:

    >
    > > > Kirk Strauser wrote:
    > > >> At 2008-11-29T04:02:11Z, Mel <> 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()
     
    Jason Scheirer, Dec 11, 2008
    #12
  13. MRAB Guest

    Jason Scheirer wrote:
    > On Dec 11, 3:49 pm, John Machin <> wrote:
    >> On Dec 12, 10:31 am, "Rhodri James" <>
    >> wrote:
    >>
    >>
    >>
    >>> On Thu, 11 Dec 2008 19:49:23 -0000, Steve Holden <>
    >>> wrote:
    >>>> Kirk Strauser wrote:
    >>>>> At 2008-11-29T04:02:11Z, Mel <> 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
     
    MRAB, Dec 12, 2008
    #13
  14. Rhodri James Guest

    On Thu, 11 Dec 2008 23:49:10 -0000, John Machin <>
    wrote:

    > On Dec 12, 10:31 am, "Rhodri James" <>
    > wrote:
    >> On Thu, 11 Dec 2008 19:49:23 -0000, Steve Holden <>  
    >> 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
     
    Rhodri James, Dec 12, 2008
    #14
  15. John Machin Guest

    On Dec 12, 11:39 am, MRAB <> wrote:
    > Jason Scheirer wrote:
    > > On Dec 11, 3:49 pm, John Machin <> wrote:
    > >> On Dec 12, 10:31 am, "Rhodri James" <>
    > >> wrote:

    >
    > >>> On Thu, 11 Dec 2008 19:49:23 -0000, Steve Holden <>  
    > >>> wrote:
    > >>>> Kirk Strauser wrote:
    > >>>>> At 2008-11-29T04:02:11Z, Mel <> 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
     
    John Machin, Dec 12, 2008
    #15
  16. MRAB Guest

    John Machin wrote:
    > On Dec 12, 11:39 am, MRAB <> wrote:
    >> Jason Scheirer wrote:
    >>> On Dec 11, 3:49 pm, John Machin <> wrote:
    >>>> On Dec 12, 10:31 am, "Rhodri James" <>
    >>>> wrote:
    >>>>> On Thu, 11 Dec 2008 19:49:23 -0000, Steve Holden <>
    >>>>> wrote:
    >>>>>> Kirk Strauser wrote:
    >>>>>>> At 2008-11-29T04:02:11Z, Mel <> 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
     
    MRAB, Dec 12, 2008
    #16
  17. John Machin Guest

    On Dec 12, 1:11 pm, MRAB <> wrote:
    > John Machin wrote:
    > > On Dec 12, 11:39 am, MRAB <> wrote:
    > >> Jason Scheirer wrote:
    > >>> On Dec 11, 3:49 pm, John Machin <> wrote:
    > >>>> On Dec 12, 10:31 am, "Rhodri James" <>
    > >>>> wrote:
    > >>>>> On Thu, 11 Dec 2008 19:49:23 -0000, Steve Holden <>  
    > >>>>> wrote:
    > >>>>>> Kirk Strauser wrote:
    > >>>>>>> At 2008-11-29T04:02:11Z, Mel <> 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 :)
     
    John Machin, Dec 12, 2008
    #17
  18. MRAB Guest

    Kirk Strauser wrote:
    > At 2008-12-12T15:35:11Z, "J. Cliff Dyer" <> 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.
     
    MRAB, Dec 12, 2008
    #18
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christopher M. Lusardi
    Replies:
    1
    Views:
    432
    Victor Bazarov
    Nov 19, 2004
  2. Christopher M. Lusardi
    Replies:
    1
    Views:
    366
    Victor Bazarov
    Nov 19, 2004
  3. malli
    Replies:
    7
    Views:
    344
    TomHanks
    Oct 21, 2005
  4. (type)*(var1)-like casting

    , Mar 16, 2010, in forum: C Programming
    Replies:
    5
    Views:
    278
  5. Luca Scaljery
    Replies:
    5
    Views:
    120
    Patrick Spence
    Feb 14, 2007
Loading...

Share This Page