Fwd: [Tutor] problem with stirng.find

Liam Clarke cyresse at gmail.com
Wed Nov 24 07:46:08 CET 2004


---------- Forwarded message ----------
From: Liam Clarke <cyresse at gmail.com>
Date: Wed, 24 Nov 2004 19:45:58 +1300
Subject: Re: [Tutor] problem with stirng.find
To: Gregor Lingl <glingl at aon.at>


In the above

>I get the following string (print s) :
>"\xffS!AAAA\xfe0.005\r\n\xffS!AAAA\xfe0.005\r\n"

>When I do

>Import string
>Print string.find(s,"S!A")

>It prints: 11

I have no idea what's happening, but if you remove the special characters - i.e.

"<\xff>S!AAAA<\xfe>0.005<\r\n><\xff>S!AAAA<\xfe>0.005<\r\n>"

x="S!AAAA0.005S!AAAA0.005"

then x[:11]="S!AAAA0.005"
and  x[11:]="S!AAAA0.005"

Go figure.




On Tue, 23 Nov 2004 08:41:48 +0100, Gregor Lingl <glingl at aon.at> wrote:
>
>
> Isr Gish schrieb:
>
> >Fabian von Berlepsch"<pub at berlepsch.de> wrote:
> >   >I am using pyserail for reading the serial port
> >   >I get the following string (print s) :
> >   >"\xffS!AAAA\xfe0.005\r\n\xffS!AAAA\xfe0.005\r\n"
> >   >
> >   >When I do
> >   >
> >   >Import string
> >
> >...
> >
> > functions in the string module, are now available as string methods.
> >
> >   >Print string.find(s,"S!A")
> >   >
> >   >It prints: 11
> >
> >The find method (function) returns the index where the substring was found. I'm not sure what you expected to happen.
> >
> >
> First I was surprised to find
>
>  >>> for c in "\xffS!AAAA\xfe0.005\r\n\xffS!AAAA\xfe0.005\r\n":
>     c,ord(c)
>
> ('\xff', 255)
> ('S', 83)
> ('!', 33)
> ('A', 65)
> ('A', 65)
> ('A', 65)
> ('A', 65)
> ('\xfe', 254)
> ('0', 48)
> ('.', 46)
> ('0', 48)
> ('0', 48)
> ('5', 53)
> ('\r', 13)
> ('\n', 10)
> ('\xff', 255)
> ('S', 83)
> ('!', 33)
> ('A', 65)
> ('A', 65)
> ('A', 65)
> ('A', 65)
> ('\xfe', 254)
> ('0', 48)
> ('.', 46)
> ('0', 48)
> ('0', 48)
> ('5', 53)
> ('\r', 13)
> ('\n', 10)
>  >>> "\xffS!AAAA\xfe0.005\r\n\xffS!AAAA\xfe0.005\r\n"[11:14]
> '05\r'
>
> So I checked if find indeed returned 11:
>
>  >>> "\xffS!AAAA\xfe0.005\r\n\xffS!AAAA\xfe0.005\r\n".find("S!A")
> 1
>  >>> "\xffS!AAAA\xfe0.005\r\n\xffS!AAAA\xfe0.005\r\n".find("S!A",2)  #
> next occurrence
> 16
>  >>>
>  That's ok, isn't it?
>
> Gregor
>
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>


--
'There is only one basic human right, and that is to do as you damn well please.
And with it comes the only ba>>>sic human duty, to take the consequences.


-- 
'There is only one basic human right, and that is to do as you damn well please.
And with it comes the only basic human duty, to take the consequences.


More information about the Tutor mailing list