linecache and comparison with input

Discussion in 'Python' started by Ross Hetherington, Jun 30, 2007.

  1. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA512

    Say I've got a file called 'testfile' that contains the following three
    lines:

    foo
    bar
    baz

    And a script that goes like this:

    #!/usr/bin/env python

    import random
    import sys
    import linecache

    rnd = random.randint(1,3)
    line = linecache.getline('testfile', rnd)

    print line

    gss = raw_input('Enter line: ',)

    if gss == line:
    print 'yes'
    sys.exit()
    else:
    print 'no'


    Even if the right word is entered, the script always prints `no'. Can
    anyone explain why this happens?
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.7 (GNU/Linux)
    Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org

    iD8DBQFGhmUm0HlvQApfM3gRCl23AJ45cv7A0KVi2h8/cWmcCrg4X8HZOACfT6V8
    IpXAzpD0jKnFFDEJEmOFvc4=
    =UA5b
    -----END PGP SIGNATURE-----
    Ross Hetherington, Jun 30, 2007
    #1
    1. Advertising

  2. [ Ross Hetherington <> ]
    > #!/usr/bin/env python
    >
    > import random
    > import sys
    > import linecache
    >
    > rnd = random.randint(1,3)
    > line = linecache.getline('testfile', rnd)
    >
    > print line

    Try print repr(line) ...
    >
    > gss = raw_input('Enter line: ',)

    and print repr(gss) ;)

    > if gss == line:
    > print 'yes'
    > sys.exit()
    > else:
    > print 'no'
    >


    Then you will see, that getline returns the line *including the newline
    character*, while raw_input does not. Use line.strip('\n') to remove
    trailing newline characters from the return value of getline.

    --
    Freedom is always the freedom of dissenters.
    (Rosa Luxemburg)

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v2.0.4 (GNU/Linux)

    iD4DBQBGhmhrn3IEGILecb4RAs4LAKCkE7r5b5CAXi3Et2eYQ0FJlWDTdgCYtMGd
    DBzXXDbb2jIOpuJD3XRokA==
    =Mmo7
    -----END PGP SIGNATURE-----
    Sebastian Wiesner, Jun 30, 2007
    #2
    1. Advertising

  3. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA512

    Sebastian Wiesner wrote:
    > Then you will see, that getline returns the line *including the newline
    > character*, while raw_input does not. Use line.strip('\n') to remove
    > trailing newline characters from the return value of getline.


    Ah, thank you. I did notice that getline left a trailing newline, but I
    didn't realise raw_input didn't. Ooops.

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.7 (GNU/Linux)
    Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org

    iD8DBQFGhml20HlvQApfM3gRCozzAJ0QAENDkQn8StlEr9Mh0yoCCkUhzQCcCmOC
    5tPusqS4MwnisNLZmtKc3Y4=
    =hlHr
    -----END PGP SIGNATURE-----
    Ross Hetherington, Jun 30, 2007
    #3
    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. jo3c

    linecache and glob

    jo3c, Jan 4, 2008, in forum: Python
    Replies:
    5
    Views:
    489
  2. R. Bernstein
    Replies:
    3
    Views:
    370
    R. Bernstein
    Dec 21, 2008
  3. Replies:
    1
    Views:
    566
    Peter Otten
    Oct 13, 2009
  4. rocky
    Replies:
    0
    Views:
    106
    rocky
    Oct 23, 2009
  5. Deepu
    Replies:
    1
    Views:
    242
    ccc31807
    Feb 7, 2011
Loading...

Share This Page