Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #37559

Re: using split for a string : error

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 <insideshoes@gmail.com>
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; '&quot;': 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; '&gt;&gt;': 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 <CAFqGZRGMXcku_nM_LqPsHpCzqGxF57LOSKwKwoCH+LyhPA8t_A@mail.gmail.com> <51011315.3060406@tobix.eu>
Date Thu, 24 Jan 2013 12:01:00 +0100
Subject Re: using split for a string : error
From inshu chauhan <insideshoes@gmail.com>
To "Tobias M." <tm@tobix.eu>
Content-Type multipart/alternative; boundary=14dae9340ddfe7544904d406b9f3
Cc "python-list@python.org" <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 <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.959.1359025269.2939.python-list@python.org> (permalink)
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

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

On Thu, Jan 24, 2013 at 11:55 AM, Tobias M. <tm@tobix.eu> 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
>
>

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: using split for a string : error inshu chauhan <insideshoes@gmail.com> - 2013-01-24 12:01 +0100

csiph-web