473,791 Members | 3,251 Online
Bytes | Software Development & Data Engineering Community
+ 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.getli ne('testfile', rnd)

print line

gss = raw_input('Ente r 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

iD8DBQFGhmUm0Hl vQApfM3gRCl23AJ 45cv7A0KVi2h8/cWmcCrg4X8HZOAC fT6V8
IpXAzpD0jKnFFDE JEmOFvc4=
=UA5b
-----END PGP SIGNATURE-----
Jun 30 '07 #1
2 1364
[ Ross Hetherington <ro**@heth.ecli pse.co.uk]
#!/usr/bin/env python

import random
import sys
import linecache

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

print line
Try print repr(line) ...
>
gss = raw_input('Ente r 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)

iD4DBQBGhmhrn3I EGILecb4RAs4LAK CkE7r5b5CAXi3Et 2eYQ0FJlWDTdgCY tMGd
DBzXXDbb2jIOpuJ D3XRokA==
=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

iD8DBQFGhml20Hl vQApfM3gRCozzAJ 0QAENDkQn8StlEr 9Mh0yoCCkUhzQCc CmOC
5tPusqS4MwnisNL ZmtKc3Y4=
=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
10231
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 Date(); if(sdate < todaysdate)
4
16241
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 loooong vector and loop in an incremental way to select the column of choice, here is the code which works but gives a warning ****************************************************************
7
1663
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 are exactly the same, but the execution result seems they are not equal. It works with firefox, but not with IE.
7
2514
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 pairwise comparison among 2500 items or so in the file. Each of those items have to be compared to every other item. Many of the comparison will only require comparing one field of the items. I will probably sort on this field before I do the...
0
1252
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
1471
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
2412
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 it doesn't work! i can't use glob wildcard with linecache
1
2663
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 locally saved. Here's what I have for saving the text file using the preferred format: Dim FN As Long FN = FreeFile Open "C:\Documents and Settings\Adam.Martin\Desktop\UNPW.txt" For Output As FN Print #FN, txtUsername.Text + ";" +...
3
4101
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 formatted like so: /beginning/ matrix1 3 2 1 6 2 4 3 5 matrix2
0
9517
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10428
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10156
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9997
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7537
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5435
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5559
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4110
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3718
muto222
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.