[Tutor] Problem Stripping

classic Classic list List threaded Threaded
13 messages Options
Reply | Threaded
Open this post in threaded view
|

[Tutor] Problem Stripping

Leam Hall
Python 2.4.3 on Red Hat 5. Trying to use strip to remove characters
but it doesn't seem to work like I thought.


res = subprocess.Popen(['uname', '-a'], stdout=subprocess.PIPE)
uname = res.stdout.read().strip()

>>> uname
'Linux myserver 2.6.18-274.el5PAE #1 SMP Fri Jul 8 17:59:09 EDT 2011
i686 i686 i386 GNU/Linux'

>>> uname.strip(':')
'Linux myserver 2.6.18-274.el5PAE #1 SMP Fri Jul 8 17:59:09 EDT 2011
i686 i686 i386 GNU/Linux'

>>> 'www.example.com'.strip('cmowz.')
'example'

Thoughts?

Leam
--
Mind on a Mission <http://leamhall.blogspot.com/>
_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] Problem Stripping

Evert Rol
> Python 2.4.3 on Red Hat 5. Trying to use strip to remove characters
> but it doesn't seem to work like I thought.
>
>
> res = subprocess.Popen(['uname', '-a'], stdout=subprocess.PIPE)
> uname = res.stdout.read().strip()
>
>>>> uname
> 'Linux myserver 2.6.18-274.el5PAE #1 SMP Fri Jul 8 17:59:09 EDT 2011
> i686 i686 i386 GNU/Linux'
>
>>>> uname.strip(':')
> 'Linux myserver 2.6.18-274.el5PAE #1 SMP Fri Jul 8 17:59:09 EDT 2011
> i686 i686 i386 GNU/Linux'
>
>>>> 'www.example.com'.strip('cmowz.')
> 'example'
>
> Thoughts?

Read the documentation carefully: http://docs.python.org/library/stdtypes.html#str.strip
It strips *any* of the characters in the strip() argument from either side of the string, up to the point where it can't strip a matching character anymore.
Since the uname variable doesn't not start with a ':' on either side, it can't strip anything, and stops immediately, returning the resulting (full) string.

You could look into split(':', maxsplit), with maxsplit not at its default value: http://docs.python.org/library/stdtypes.html#str.split
Otherwise, regular experssions would be a (bit more drastic) solution.

Btw, what *did* you actually expect it should do? That would give us a better hint into the result you want.

Good luck,

  Evert

 
>
> Leam
> --
> Mind on a Mission <http://leamhall.blogspot.com/>
> _______________________________________________
> Tutor maillist  -  [hidden email]
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] Problem Stripping

Prasad, Ramit-2
In reply to this post by Leam Hall

> Python 2.4.3 on Red Hat 5. Trying to use strip to remove characters
> but it doesn't seem to work like I thought.
>
>
> res = subprocess.Popen(['uname', '-a'], stdout=subprocess.PIPE)
> uname = res.stdout.read().strip()
>
> >>> uname
> 'Linux myserver 2.6.18-274.el5PAE #1 SMP Fri Jul 8 17:59:09 EDT 2011
> i686 i686 i386 GNU/Linux'
>
> >>> uname.strip(':')
> 'Linux myserver 2.6.18-274.el5PAE #1 SMP Fri Jul 8 17:59:09 EDT 2011
> i686 i686 i386 GNU/Linux'
>
> >>> 'www.example.com'.strip('cmowz.')
> 'example'
>
> Thoughts?

Read the documentation. It is excellent and explains this behavior.
http://docs.python.org/library/string.html

If you are trying to remove ':' from the result of uname what you
really want is uname.replace(':','')

Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423

--

This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] Problem Stripping

Joel Goldstick-2
In reply to this post by Leam Hall
On Fri, Mar 30, 2012 at 1:09 PM, leam hall <[hidden email]> wrote:

> Python 2.4.3 on Red Hat 5. Trying to use strip to remove characters
> but it doesn't seem to work like I thought.
>
>
> res = subprocess.Popen(['uname', '-a'], stdout=subprocess.PIPE)
> uname = res.stdout.read().strip()
>
>>>> uname
> 'Linux myserver 2.6.18-274.el5PAE #1 SMP Fri Jul 8 17:59:09 EDT 2011
> i686 i686 i386 GNU/Linux'
>
>>>> uname.strip(':')
> 'Linux myserver 2.6.18-274.el5PAE #1 SMP Fri Jul 8 17:59:09 EDT 2011
> i686 i686 i386 GNU/Linux'
>
>>>> 'www.example.com'.strip('cmowz.')
> 'example'
>
> Thoughts?
>
> Leam
> --
> Mind on a Mission <http://leamhall.blogspot.com/>
> _______________________________________________
> Tutor maillist  -  [hidden email]
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

str.strip() removes characters from begining and end of the string --
Not any in between.  Notice

>>> example = "www.example.com".strip("wcomx")
>>> example
'.example.'
>>>

The x remains


--
Joel Goldstick
_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] Problem Stripping

Joel Goldstick-2
On Fri, Mar 30, 2012 at 1:25 PM, Joel Goldstick
<[hidden email]> wrote:

> On Fri, Mar 30, 2012 at 1:09 PM, leam hall <[hidden email]> wrote:
>> Python 2.4.3 on Red Hat 5. Trying to use strip to remove characters
>> but it doesn't seem to work like I thought.
>>
>>
>> res = subprocess.Popen(['uname', '-a'], stdout=subprocess.PIPE)
>> uname = res.stdout.read().strip()
>>
>>>>> uname
>> 'Linux myserver 2.6.18-274.el5PAE #1 SMP Fri Jul 8 17:59:09 EDT 2011
>> i686 i686 i386 GNU/Linux'
>>
>>>>> uname.strip(':')
>> 'Linux myserver 2.6.18-274.el5PAE #1 SMP Fri Jul 8 17:59:09 EDT 2011
>> i686 i686 i386 GNU/Linux'
>>
>>>>> 'www.example.com'.strip('cmowz.')
>> 'example'
>>
>> Thoughts?
>>
>> Leam
>> --
>> Mind on a Mission <http://leamhall.blogspot.com/>
>> _______________________________________________
>> Tutor maillist  -  [hidden email]
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
>
> str.strip() removes characters from begining and end of the string --
> Not any in between.  Notice
>
>>>> example = "www.example.com".strip("wcomx")
>>>> example
> '.example.'
>>>>
>
> The x remains


You could do list comprehension
>
>>> n = "".join([x for x in "this has : some : colons" if x not in ':'])
>>> n
'this has  some  colons'
>
--
Joel Goldstick
_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] Problem Stripping

Emile van Sebille
In reply to this post by Leam Hall
On 3/30/2012 10:09 AM leam hall said...
> Python 2.4.3 on Red Hat 5. Trying to use strip to remove characters
> but it doesn't seem to work like I thought.


... but it works as advertised...

Help on built-in function strip:

strip(...)
     S.strip([chars]) -> string or unicode

     Return a copy of the string S with leading and trailing
     whitespace removed.
     If chars is given and not None, remove characters in chars instead.
     If chars is unicode, S will be converted to unicode before stripping



If you're expecting the ":"s to be stripped out, try using replace:

Help on built-in function replace:

replace(...)
     S.replace (old, new[, count]) -> string

     Return a copy of string S with all occurrences of substring
     old replaced by new.  If the optional argument count is
     given, only the first count occurrences are replaced.


HTH,

Emile

>
>
> res = subprocess.Popen(['uname', '-a'], stdout=subprocess.PIPE)
> uname = res.stdout.read().strip()
>
>>>> uname
> 'Linux myserver 2.6.18-274.el5PAE #1 SMP Fri Jul 8 17:59:09 EDT 2011
> i686 i686 i386 GNU/Linux'
>
>>>> uname.strip(':')
> 'Linux myserver 2.6.18-274.el5PAE #1 SMP Fri Jul 8 17:59:09 EDT 2011
> i686 i686 i386 GNU/Linux'
>
>>>> 'www.example.com'.strip('cmowz.')
> 'example'
>
> Thoughts?
>
> Leam




_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] Problem Stripping

Prasad, Ramit-2
> strip(...)
>      S.strip([chars]) -> string or unicode
>
>      Return a copy of the string S with leading and trailing
>      whitespace removed.
>      If chars is given and not None, remove characters in chars instead.
>      If chars is unicode, S will be converted to unicode before stripping

You can get this text on the interpreter by doing help(str.strip) or
help(''.strip). There is even an interactive help you can access by doing
help(). I find it very useful, although usefulness varies with 3rd party
code.

Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423

--
This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] Problem Stripping

Mark Lawrence
In reply to this post by Leam Hall
On 30/03/2012 18:09, leam hall wrote:
> Python 2.4.3 on Red Hat 5. Trying to use strip to remove characters
> but it doesn't seem to work like I thought.

What do you expect it to do?

>
>
> res = subprocess.Popen(['uname', '-a'], stdout=subprocess.PIPE)
> uname = res.stdout.read().strip()
>
>>>> uname
> 'Linux myserver 2.6.18-274.el5PAE #1 SMP Fri Jul 8 17:59:09 EDT 2011
> i686 i686 i386 GNU/Linux'
>
>>>> uname.strip(':')
> 'Linux myserver 2.6.18-274.el5PAE #1 SMP Fri Jul 8 17:59:09 EDT 2011
> i686 i686 i386 GNU/Linux'
>
>>>> 'www.example.com'.strip('cmowz.')
> 'example'
>
> Thoughts?
>
> Leam

Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
 >>> help(''.strip)
Help on built-in function strip:

strip(...)
     S.strip([chars]) -> string or unicode

     Return a copy of the string S with leading and trailing
     whitespace removed.
     If chars is given and not None, remove characters in chars instead.
     If chars is unicode, S will be converted to unicode before stripping

So it appears that your examples are doing precisely what is defined above.

HTH.

--
Cheers.

Mark Lawrence.

_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] Problem Stripping

Mark Lawrence
In reply to this post by Joel Goldstick-2
On 30/03/2012 18:28, Joel Goldstick wrote:

> On Fri, Mar 30, 2012 at 1:25 PM, Joel Goldstick
> <[hidden email]>  wrote:
>> On Fri, Mar 30, 2012 at 1:09 PM, leam hall<[hidden email]>  wrote:
>>> Python 2.4.3 on Red Hat 5. Trying to use strip to remove characters
>>> but it doesn't seem to work like I thought.
>>>
>>>
>>> res = subprocess.Popen(['uname', '-a'], stdout=subprocess.PIPE)
>>> uname = res.stdout.read().strip()
>>>
>>>>>> uname
>>> 'Linux myserver 2.6.18-274.el5PAE #1 SMP Fri Jul 8 17:59:09 EDT 2011
>>> i686 i686 i386 GNU/Linux'
>>>
>>>>>> uname.strip(':')
>>> 'Linux myserver 2.6.18-274.el5PAE #1 SMP Fri Jul 8 17:59:09 EDT 2011
>>> i686 i686 i386 GNU/Linux'
>>>
>>>>>> 'www.example.com'.strip('cmowz.')
>>> 'example'
>>>
>>> Thoughts?
>>>
>>> Leam
>>> --
>>> Mind on a Mission<http://leamhall.blogspot.com/>
>>> _______________________________________________
>>> Tutor maillist  -  [hidden email]
>>> To unsubscribe or change subscription options:
>>> http://mail.python.org/mailman/listinfo/tutor
>>
>> str.strip() removes characters from begining and end of the string --
>> Not any in between.  Notice
>>
>>>>> example = "www.example.com".strip("wcomx")
>>>>> example
>> '.example.'
>>>>>
>>
>> The x remains
>
>
> You could do list comprehension
>>
>>>> n = "".join([x for x in "this has : some : colons" if x not in ':'])
>>>> n
> 'this has  some  colons'
>>

Yuck :(

Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
 >>> "this has : some : colons".replace(':', '')
'this has  some  colons'

--
Cheers.

Mark Lawrence.

_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] Problem Stripping

Bob Gailer
Then, of course, there's "15:45".replace(':','')

--
Bob Gailer
919-636-4239
Chapel Hill NC

_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] Problem Stripping

Mark Lawrence
On 31/03/2012 03:01, bob gailer wrote:
> Then, of course, there's "15:45".replace(':','')
>

For the record in earlier Python versions such as the one the OP is
using you can do

 >>> allchars = "".join([chr(x) for x in range(256)])
 >>> 'www.example.com'.translate(allchars, 'cmowz.')
'exaple'

As of Python 2.6 you don't even need the allchars hence

 >>> 'www.example.com'.translate(None, 'cmowz.')
'exaple'

--
Cheers.

Mark Lawrence.

_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] Problem Stripping

Steven D'Aprano-8
In reply to this post by Leam Hall
leam hall wrote:
> Python 2.4.3 on Red Hat 5. Trying to use strip to remove characters
> but it doesn't seem to work like I thought.
>
>
> res = subprocess.Popen(['uname', '-a'], stdout=subprocess.PIPE)
> uname = res.stdout.read().strip()

For future reference, you should identify the shortest possible amount of code
that demonstrates the problem. See also http://sscce.org/

You are asking a question about string.strip(). Where the string comes from is
irrelevant -- there's no need to use an example as complicated as the above,
when a simple string constant will do the job perfectly. So the shortest
possible amount of code to demonstrate your problem is a single method call:

 >>> 'spam ham 1:2:3 eggs'.strip(':')
'spam ham 1:2:3 eggs'


All the stuff with subprocess.Popen and reading from stout and whatnot doesn't
have anything to do with the issue at hand. It cannot shed any light on the
problem; at best it must be ignored, at worst it may confuse the issue.

As others have already explained, strip() does not remove characters from
anywhere in the string, it strips them from the ends only.

There is also a lstrip() and rstrip() for times you only want to remove them
from the left or right side.



--
Steven

_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] Problem Stripping

Steven D'Aprano-8
Please keep your reply on the list, unless you have something private to say.
That way others can help, or learn from your questions. I've taken the liberty
of putting this back into the list.


Leam Hall wrote:

> On 04/01/2012 09:40 PM, Steven D'Aprano wrote:
>> leam hall wrote:
>>> Python 2.4.3 on Red Hat 5. Trying to use strip to remove characters
>>> but it doesn't seem to work like I thought.
>>>
>>>
>>> res = subprocess.Popen(['uname', '-a'], stdout=subprocess.PIPE)
>>> uname = res.stdout.read().strip()
>>
>> For future reference, you should identify the shortest possible amount
>> of code that demonstrates the problem. See also http://sscce.org/
>>
>> You are asking a question about string.strip(). Where the string comes
>> from is irrelevant -- there's no need to use an example as complicated
>> as the above, when a simple string constant will do the job perfectly.
>> So the shortest possible amount of code to demonstrate your problem is a
>> single method call:
>>
>>  >>> 'spam ham 1:2:3 eggs'.strip(':')
>> 'spam ham 1:2:3 eggs'
>>
>>
>> All the stuff with subprocess.Popen and reading from stout and whatnot
>> doesn't have anything to do with the issue at hand. It cannot shed any
>> light on the problem; at best it must be ignored, at worst it may
>> confuse the issue.
>>
>> As others have already explained, strip() does not remove characters
>> from anywhere in the string, it strips them from the ends only.
>>
>> There is also a lstrip() and rstrip() for times you only want to remove
>> them from the left or right side.
>
> Steve,
>
> How does one minimize the example code when one doesn't know the
> problem? The short example seemed to work, the long one didn't. So the
> options seemed to be that the long code changed the expected behavior,
> the long code was transcribed incorrectly, or I misunderstood something.
> At the point of asking the question I didn't know which of those was the
> issue.

That's a good question. Being able to diagnose errors or unexpected behaviour
is an absolutely critical skill for a programmer. So how could you have
diagnosed this (apparent) error?

(Apart from reading the Fine Manual, of course.)

You started off by reading some data with the subprocess module. So the first
thing to do is to replace the calls

res = subprocess.Popen(['uname', '-a'], stdout=subprocess.PIPE)
uname = res.stdout.read().strip()

with a single assignment using the exact same string:

uname = 'Linux ando 2.6.18-53.el5 #1 SMP Mon Nov 12 02:22:48 EST 2007 i686
i686 i386 GNU/Linux'

(The above should be a single line; my mail client wraps it over two lines.)

Does the problem still exist? Yes:

 >>> uname.strip(':')  # I expect the colons to disappear.
'Linux ando 2.6.18-53.el5 #1 SMP Mon Nov 12 02:22:48 EST 2007 i686 i686 i386
GNU/Linux'

And lo, the colons don't disappear. The "problem" (as you saw it) continues.
Clearly the problem has nothing to do with the *source* of the string. (It
would be a bizarre and strange situation if it did!)

Step two: Simplify simplify simplify. Why use 85 characters when fewer than a
dozen will do? Cut out all the irrelevant bits of the string:

 >>> uname = '02:22:48'
 >>> uname.strip(':')
'02:22:48'

No change in the mysterious behaviour.

At this point you have your short, simple example:

 >>> '02:22:48'.strip(':')  # I expect '022248'
'02:22:48'


and you can ask for help. Or if you are keen to solve this yourself, you could
try different strings and see if you can work out what's going on:

 >>> '0222:48'.strip(':')  # maybe it works with only 1 colon?
'0222:48'

 >>> '022248:'.strip(':')  # or perhaps if I move the colon somewhere else?
'022248'

 >>> ':02:22:48:'.strip(':')  # which colons will be removed?
'02:22:48'




--
Steven

_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor