There's got to be an easy way to do this

Chris Barker chrishbarker at home.net
Thu Jul 5 16:49:42 EDT 2001


> > > I am reading in a phone number field and would like to throw away
> everything
> > > except the digits.  Being an old C programmer, I know how I would do
> this.

> > > but with all of the data structure support native to Python, I'm sure
> there
> > > is "an obvious way" to do it (perhaps it's because I'm not Dutch:-).

I havn't see this whole thread, but did anyone suggest the translate
string method?:

>>> table = "".join([chr(x) for x in range(256)])
>>> remove = table.replace("0123456789","")
>>> "555-343:564-othergarbage987".translate(table,remove)
'555343564987'

I imagine it's a lot faster that REs. (I'm only imagining).

Note: does anyone know if there is a way to use the delete part without
the translate part?

-Chris

-- 
Christopher Barker,
Ph.D.                                                           
ChrisHBarker at home.net                 ---           ---           ---
http://members.home.net/barkerlohmann ---@@       -----@@       -----@@
                                   ------@@@     ------@@@     ------@@@
Oil Spill Modeling                ------   @    ------   @   ------   @
Water Resources Engineering       -------      ---------     --------    
Coastal and Fluvial Hydrodynamics --------------------------------------
------------------------------------------------------------------------



More information about the Python-list mailing list