Removing Space and "-" from a string

inhahe inhahe at gmail.com
Wed May 21 01:58:23 EDT 2008


<python at bdurham.com> wrote in message 
news:mailman.1387.1211301238.12834.python-list at python.org...
> Shakir,
>
>> I have thousands of records in MS Access database table, which records I
>> am fetching using python script. One of the columns having string like
>> '8 58-2155-58'
>>
>> Desired output: '858215558'
>>
>> I want to remove any spaces between string and any dashes between
>> strings. I could do it in access manually but want to do from python
>> script
>
> Try this:
>
>>>> input = '8 58-2155-58'
>>>> output = ''.join( [ c for c in input if c not in ' -' ] )
>>>> output
> '858215558'
>
> Malcolm

how about

output = ''.join(input.replace('-',' ').split())
or
output = input.replace('-','').replace(' ','')





More information about the Python-list mailing list