Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

Removing Space and "-" from a string

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


shahmed at sfwmd

May 20, 2008, 9:02 AM

Post #1 of 8 (14942 views)
Permalink
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


s0suk3 at gmail

May 20, 2008, 9:22 AM

Post #2 of 8 (14884 views)
Permalink
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


python at bdurham

May 20, 2008, 9:33 AM

Post #3 of 8 (14884 views)
Permalink
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


shahmed at sfwmd

May 20, 2008, 9:38 AM

Post #4 of 8 (14885 views)
Permalink
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 [at] python
[mailto:python-list-bounces+shahmed=sfwmd.gov [at] python] On Behalf Of
s0suk3 [at] gmail
Sent: Tuesday, May 20, 2008 12:22 PM
To: python-list [at] python
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


Scott.Daniels at acm

May 20, 2008, 6:08 PM

Post #5 of 8 (14873 views)
Permalink
Re: Removing Space and "-" from a string [In reply to]

python [at] bdurham 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
--
http://mail.python.org/mailman/listinfo/python-list


inhahe at gmail

May 20, 2008, 10:58 PM

Post #6 of 8 (14869 views)
Permalink
Re: Removing Space and "-" from a string [In reply to]

<python [at] bdurham> wrote in message
news:mailman.1387.1211301238.12834.python-list [at] python
> 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


paul.hankin at gmail

May 20, 2008, 11:47 PM

Post #7 of 8 (14872 views)
Permalink
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


tjreedy at udel

May 21, 2008, 12:38 PM

Post #8 of 8 (14869 views)
Permalink
Re: Removing Space and "-" from a string [In reply to]

"Paul Hankin" <paul.hankin [at] gmail> wrote in message
news:9d9dadfe-3f91-44a6-8b1b-50fe8003046d [at] e39g2000hsf
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

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.