Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'received:209.85.223': 0.03; 'f.close()': 0.07; 'skip:o 50': 0.07; 'trailing': 0.07; '#print': 0.09; '"': 0.09; 'be:': 0.09; 'correct,': 0.09; 'expected.': 0.09; 'integers': 0.09; 'skip:o 60': 0.09; 'subject:string': 0.09; 'subject:using': 0.09; 'whitespaces': 0.09; 'cc:addr:python-list': 0.10; 'subject:error': 0.11; 'file,': 0.15; '>>': 0.16; '24,': 0.16; 'csv': 0.16; 'wrote:': 0.17; 'basically': 0.17; 'thu,': 0.17; 'tries': 0.17; 'jan': 0.18; 'file.': 0.20; 'cc:2**0': 0.23; 'split': 0.23; 'tried': 0.25; 'least': 0.25; 'cc:addr:python.org': 0.25; 'header :In-Reply-To:1': 0.25; 'am,': 0.27; 'message-id:@mail.gmail.com': 0.27; 'behaving': 0.29; 'url:mailman': 0.29; 'skip:& 10': 0.29; 'probably': 0.29; 'worked': 0.30; 'subject: : ': 0.30; 'code': 0.31; 'url:python': 0.32; 'could': 0.32; 'print': 0.32; 'url:listinfo': 0.32; 'function.': 0.33; 'equal': 0.33; 'hi,': 0.33; 'received:google.com': 0.34; 'received:209.85': 0.35; 'there': 0.35; 'but': 0.36; 'url:org': 0.36; 'characters': 0.36; 'compare': 0.36; 'thank': 0.36; 'why': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'skip:l 20': 0.38; 'little': 0.39; 'url:mail': 0.40; 'think': 0.40; 'your': 0.60; 'skip:u 10': 0.60; 'remove': 0.61; 'leading': 0.61; '!!!': 0.62; 'different': 0.63; 'here': 0.65; 'results': 0.65; 'programme': 0.69; 'printing,': 0.75; '11:': 0.84; '2013': 0.84; 'is\xa0': 0.84; '\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0': 0.84; '\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:cc:content-type; bh=yffsAVi2z8mxEPTXkzr8KYqrR1yLn80ngnk98hZZDK4=; b=DpKtHra5ouB8b0vp9tC98ElFUZCCDqN3AgLrKhBuAQofAaXZNYDbQ3sB9h4rDI5G+X vElHDohrEfsmKt5RKsW4ZVhm1wD2h+zdpcTHggH4dTiYgSi+yTp60UHdqhLQvvLcs+Si YFuMXK476ZBwpaMr1x0AFWo6wANz4cXUqp61YOkZm3tAQNCYPfa6z5+SI2I1g2diNP4W p6Tb/v5Kd4+T60ZfPSVNSSUIDZFoyZvNAZvbpa5kAducVncEtbLRtvoJJg3kpIx7OSxl sbC3Wjn5BG8YNSBCUnujGpKi+nDicSBaLwuIspOnqVST0az7JizWPSJ8L1z7HCYCBGQm ArXw== MIME-Version: 1.0 X-Received: by 10.50.184.229 with SMTP id ex5mr935511igc.72.1359025260680; Thu, 24 Jan 2013 03:01:00 -0800 (PST) In-Reply-To: <51011315.3060406@tobix.eu> References: <51011315.3060406@tobix.eu> Date: Thu, 24 Jan 2013 12:01:00 +0100 Subject: Re: using split for a string : error From: inshu chauhan To: "Tobias M." Content-Type: multipart/alternative; boundary=14dae9340ddfe7544904d406b9f3 Cc: "python-list@python.org" X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 157 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1359025269 news.xs4all.nl 6930 [2001:888:2000:d::a6]:34766 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:37559 --14dae9340ddfe7544904d406b9f3 Content-Type: text/plain; charset=ISO-8859-1 On Thu, Jan 24, 2013 at 11:55 AM, Tobias M. wrote: > Hi, > > do a "print sp" after the split and you might see that the strings don't > look as you expected. There might be leading or trailing whitespaces in the > splitted strings and in sp[10] there probably is a line break "\n" at the > end. > To remove those unwanted characters you could use the strip() function. > > So your code could be: > > if sp[9].strip() == sp[10].strip(): > > print "Same class" > else: > print "Different class" > > At least this works for me when I tried it... > > Am 24.01.2013 11:37, schrieb inshu chauhan: > > Here I have a code which basically reads a csv file, tries to compare > the last 2 items in each line of the file. > > f = open(r"Z:\modules\Feature_Vectors_300_Pclass.arff") > for l in f: > sp = l.split(",") > if len(sp) != 11: > print >> of, l, > > else: > #print sp[9], sp[10] > if sp[9] == sp[10]: > print " Same class" > else : > print "Different class" > > f.close() > > For me I think the programme is logically correct, but its giving me > results which are strange. > It is Printing " Different Class" even when sp[9] is equal to sp[10] > and "Same class" when sp[9] is not equal to sp[10]. and sp[9] and sp[10] > are simple integers like 3, 3, 4 ,4. > > I have a little understanding why the programme is behaving like this ? > > > Yeah I tried printing, there were trailing white spaces, so i used > strip() and IT Worked !!! :) > Thank you > > -- > http://mail.python.org/mailman/listinfo/python-list > > --14dae9340ddfe7544904d406b9f3 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable



On Thu, Jan 24, 2013 at 11:55 AM, Tobias M. <<= a href=3D"mailto:tm@tobix.eu" target=3D"_blank">tm@tobix.eu> = wrote:
=20 =20 =20
Hi,

do a "print sp" after the split and you might see that the = strings don't look as you expected. There might be leading or trailing whitespaces in the splitted strings and in sp[10] there probably is a line break "\n" at the end.
To remove those unwanted characters you could use the strip() function.

So your code could be:

if sp[9].strip() =3D=3D sp[10].strip():

=A0=A0=A0 print "Same class"
else:
=A0=A0=A0 print "Different class"

At least this works for me when I tried it...

Am 24.01.2013 11:37, schrieb inshu chauhan:
Here I have a code which basically reads a csv file, tries to compare the last 2 items in each line of the file.

f =3D open(r"Z:\modules\Feature_Vectors_300_Pclass.arff&= quot;)
for l in f:
=A0=A0=A0 sp =3D l.split(",")
=A0=A0=A0 if len(sp) !=3D 11:
=A0=A0=A0=A0=A0=A0=A0 print >> of, l,
=A0=A0=A0=A0=A0=A0=A0
=A0=A0=A0 else:
=A0=A0=A0=A0=A0=A0=A0 #print sp[9], sp[10]
=A0=A0=A0=A0=A0=A0=A0 if sp[9] =3D=3D sp[10]:
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 print " Same class&quo= t;
=A0=A0=A0=A0=A0=A0=A0 else :
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 print "Different class= "
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0
f.close()

For me I think the programme is logically correct, but its giving me results which are strange.
It is=A0 Printing " Different Class"=A0 even when sp[9]= is equal to sp[10] and "Same class" when sp[9] is not equal to s= p[10].=A0 and sp[9] and sp[10] are simple integers like 3, 3, 4 ,4.

I have a little understanding why the programme is behaving like this ?


Yeah I tried printing, there were trailing white spaces, so i used stri= p() and IT Worked !!! :)

<= div>Thank you=A0

--
http://mail.python.org/mailman/listinfo/python-list


--14dae9340ddfe7544904d406b9f3--