linecache and comparison with input

Sebastian Wiesner basti.wiesner at gmx.net
Sat Jun 30 10:27:50 EDT 2007


[ Ross Hetherington <ross at heth.eclipse.co.uk> ]
> #!/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)
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 196 bytes
Desc: This is a digitally signed message part.
URL: <http://mail.python.org/pipermail/python-list/attachments/20070630/3c607e47/attachment.sig>


More information about the Python-list mailing list