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

Jeffery D. Collins jcollins at boulder.net
Thu Jul 5 12:49:03 EDT 2001


>>> import re
>>> re.sub("[^0-9]", "", "(555) 333.2221")
'5553332221'

or 

>>> c = re.compile("[^0-9]")
>>> c.sub("", "(555) 333.2221")
'5553332221'


On Thu, Jul 05, 2001 at 11:15:38AM -0500, Lindstrom Greg - glinds wrote:
> 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.
> Even with my limited knowledge of Python, I know how I would do it:
> 
> 	stripped_phone=''
> 	for c in phone_number:
> 		if c in digits: 
> 			stripped_phone += c
> 
> 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:-).
> 
> Greg Lindstrom
> Acxiom Corporation,                    mail: CWY10011149
> InfoBase Products Development                  office: (501) 342-1626
> 301 Industrial Blvd, Conway, AR, 72032     fax:     (501) 336-3911 
> email: Greg.Lindstrom at acxiom.com           
> 
> "When the solution is simple, God has spoken"
> 
>                                                             Albert Einstein

-- 
Jeffery Collins (http://www.boulder.net/~jcollins)




More information about the Python-list mailing list