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

Emile van Sebille emile at fenx.com
Thu Jul 5 13:15:09 EDT 2001


Or (without re):

print "".join([x for x in '(123)/456-7890' if x in '0123456789'])

--

Emile van Sebille
emile at fenx.com

---------
"Jeffery D. Collins" <jcollins at boulder.net> wrote in message
news:mailman.994351690.22119.python-list at python.org...
> >>> 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