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


Groups > comp.lang.python > #37556

Re: using split for a string : error

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!cs.uu.nl!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <tm@tobix.eu>
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; 'f.close()': 0.07; 'skip:o 50': 0.07; 'trailing': 0.07; '#print': 0.09; 'be:': 0.09; 'correct,': 0.09; 'expected.': 0.09; 'integers': 0.09; 'subject:string': 0.09; 'subject:using': 0.09; 'whitespaces': 0.09; 'subject:error': 0.11; 'file,': 0.15; '&gt;&gt;': 0.16; 'csv': 0.16; 'received:188.40.28': 0.16; 'received:your- server.de': 0.16; 'basically': 0.17; 'tries': 0.17; 'file.': 0.20; 'split': 0.23; 'tried': 0.25; 'least': 0.25; 'header:In-Reply- To:1': 0.25; 'header:User-Agent:1': 0.26; 'behaving': 0.29; 'probably': 0.29; 'subject: : ': 0.30; 'code': 0.31; 'could': 0.32; 'print': 0.32; 'function.': 0.33; 'to:addr:python-list': 0.33; 'equal': 0.33; 'hi,': 0.33; 'there': 0.35; 'but': 0.36; 'characters': 0.36; 'compare': 0.36; 'why': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'little': 0.39; 'think': 0.40; 'your': 0.60; 'skip:u 10': 0.60; 'remove': 0.61; 'leading': 0.61; 'different': 0.63; 'here': 0.65; 'results': 0.65; 'programme': 0.69; '11:': 0.84
Date Thu, 24 Jan 2013 11:55:17 +0100
From "Tobias M." <tm@tobix.eu>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130106 Thunderbird/17.0.2
MIME-Version 1.0
To python-list@python.org
Subject Re: using split for a string : error
References <CAFqGZRGMXcku_nM_LqPsHpCzqGxF57LOSKwKwoCH+LyhPA8t_A@mail.gmail.com>
In-Reply-To <CAFqGZRGMXcku_nM_LqPsHpCzqGxF57LOSKwKwoCH+LyhPA8t_A@mail.gmail.com>
Content-Type multipart/alternative; boundary="------------020406010605060201000301"
X-Authenticated-Sender tm@tobix.eu
X-Virus-Scanned Clear (ClamAV 0.97.5/16562/Thu Jan 24 00:40:16 2013)
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.956.1359024926.2939.python-list@python.org> (permalink)
Lines 128
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1359024926 news.xs4all.nl 6921 [2001:888:2000:d::a6]:57860
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:37556

Show key headers only | View raw


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

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 ?
>
>

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


Thread

Re: using split for a string : error "Tobias M." <tm@tobix.eu> - 2013-01-24 11:55 +0100

csiph-web