Removing Space and "-" from a string

Scott David Daniels Scott.Daniels at Acm.Org
Tue May 20 21:08:00 EDT 2008


python at bdurham.com wrote:
> Shakir,
> 
>> I have thousands of records in MS Access database table, which records I
>> am fetching using python script. [A] columns has strings like '8 58-2155-58'
>> Desired output: '858215558'
>>>> input = '8 58-2155-58'
>>>> output = ''.join( [ c for c in input if c not in ' -' ] )
>>>> output
> '858215558'

If you are planning to do a lot of strings:

     identity_trans = ''.join(chr(x) for x in range(256))

Then you can do simply:
     input = '8 58-2155-58'
     output = input.translate(identity_trans, ' -')

--Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Python-list mailing list