[Tutor] Problem Stripping

Steven D'Aprano steve at pearwood.info
Mon Apr 2 13:08:27 CEST 2012


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



More information about the Tutor mailing list