473,471 Members | 1,707 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

linecache and comparison with input

-----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-----
Jun 30 '07 #1
2 1352
[ Ross Hetherington <ro**@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)

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

iD4DBQBGhmhrn3IEGILecb4RAs4LAKCkE7r5b5CAXi3Et2eYQ0 FJlWDTdgCYtMGd
DBzXXDbb2jIOpuJD3XRokA==
=Mmo7
-----END PGP SIGNATURE-----

Jun 30 '07 #2
-----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

iD8DBQFGhml20HlvQApfM3gRCozzAJ0QAENDkQn8StlEr9Mh0y oCCkUhzQCcCmOC
5tPusqS4MwnisNLZmtKc3Y4=
=hlHr
-----END PGP SIGNATURE-----
Jun 30 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Tiya | last post by:
Hi there !!! I would like to know how to compare dates in javascript. var sdate = new Date(theform.SubmissionDate.value); var odate = new Date(theform.StartDate.value); var todaysdate = new...
4
by: Gary Wessle | last post by:
Hi I am writing a code to open a space delimited data file, return the number of rows and columns, as well as return the nth column where n is user define number. I put all the cells in a...
7
by: db | last post by:
Hi@all Just got a comparison problem with javascript. I want to compare : document.getElementById(currentID).getAttribute("style") with a string, e.g: "background-color: lightgreen;" They...
7
by: Alan | last post by:
Hi. I have programmed in C++ before, but I`m a couple of years out of practice. I am seeking some advice on getting started on a quickie project. . . . I have to read a 54MB text file and do a...
0
by: AWHF | last post by:
hey, is there anyone who know how to deal with vector? i have 2 files as below: file1.doc ::component list ::Start green in grp1 blue in grp2
12
by: curiousEngine | last post by:
Write a program that allows you to input 3 integer values A, B and C and output the largest of the 3 values. //TO DO RIGOUROUS TESTING #include<iostream> using namespace std; void main(){
5
by: jo3c | last post by:
hi everyone happy new year! im a newbie to python i have a question by using linecache and glob how do i read a specific line from a file in a batch and then insert it into database? because...
1
risk32
by: risk32 | last post by:
Hello. I'm trying to open a file for comparison to two (2) input box text values. ie: txtPassword.text + ";" + txtUsername.text must equal whats in a line in the UNPW (username password) text file...
3
by: itmfl | last post by:
We are writing a program that multiplies two matrices of size n x m and m x n together. The matrices are stored in a file. The user provides the filename in the command line prompt. The file is...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.