[Tutor] Problem Stripping

Evert Rol evert.rol at gmail.com
Fri Mar 30 19:24:31 CEST 2012


> 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  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list