Mailing List Archive

Removing Space and "-" from a string
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

Any help is highly appreciated.

sh
--
http://mail.python.org/mailman/listinfo/python-list
Re: Removing Space and "-" from a string [ In reply to ]
On May 20, 11:02 am, "Ahmed, Shakir" <shah...@sfwmd.gov> wrote:
> 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
>
> Any help is highly appreciated.

string.replace('-', '').replace(' ', '')
--
http://mail.python.org/mailman/listinfo/python-list
Re: Removing Space and "-" from a string [ In reply to ]
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
--
http://mail.python.org/mailman/listinfo/python-list
RE: Removing Space and "-" from a string [ In reply to ]
Thanks, works exactly what I needed.

-----Original Message-----
From: python-list-bounces+shahmed=sfwmd.gov@python.org
[mailto:python-list-bounces+shahmed=sfwmd.gov@python.org] On Behalf Of
s0suk3@gmail.com
Sent: Tuesday, May 20, 2008 12:22 PM
To: python-list@python.org
Subject: Re: Removing Space and "-" from a string

On May 20, 11:02 am, "Ahmed, Shakir" <shah...@sfwmd.gov> wrote:
> 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
>
> Any help is highly appreciated.

string.replace('-', '').replace(' ', '')
--
http://mail.python.org/mailman/listinfo/python-list

--
http://mail.python.org/mailman/listinfo/python-list
Re: Removing Space and "-" from a string [ In reply to ]
python@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@Acm.Org
--
http://mail.python.org/mailman/listinfo/python-list
Re: Removing Space and "-" from a string [ In reply to ]
<python@bdurham.com> wrote in message
news:mailman.1387.1211301238.12834.python-list@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(' ','')


--
http://mail.python.org/mailman/listinfo/python-list
Re: Removing Space and "-" from a string [ In reply to ]
On May 20, 5:02 pm, "Ahmed, Shakir" <shah...@sfwmd.gov> wrote:
> 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

'filter' returns a string if it's argument is a string, so works
nicely here.

def cleanup(s):
return filter(lambda x: x not in ' -', s)

--
Paul Hankin
--
http://mail.python.org/mailman/listinfo/python-list
Re: Removing Space and "-" from a string [ In reply to ]
"Paul Hankin" <paul.hankin@gmail.com> wrote in message
news:9d9dadfe-3f91-44a6-8b1b-50fe8003046d@e39g2000hsf.googlegroups.com...
On May 20, 5:02 pm, "Ahmed, Shakir" <shah...@sfwmd.gov> wrote:
> 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'

|def cleanup(s):
| return filter(lambda x: x not in ' -', s)

Or
>>> s='8 58-2155-58'
>>> t=str.maketrans('','',' -')
>>> s.translate(t)
'858215558'




--
http://mail.python.org/mailman/listinfo/python-list