Removing Space and "-" from a string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ahmed, Shakir

    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
  • s0suk3@gmail.com

    #2
    Re: Removing Space and "-" from a string

    On May 20, 11:02 am, "Ahmed, Shakir" <shah...@sfwmd. govwrote:
    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(' ', '')

    Comment

    • Ahmed, Shakir

      #3
      RE: Removing Space and &quot;-&quot; from a string

      Thanks, works exactly what I needed.

      -----Original Message-----
      From: python-list-bounces+shahmed =sfwmd.gov@pyth on.org
      [mailto:python-list-bounces+shahmed =sfwmd.gov@pyth on.org] On Behalf Of
      s0suk3@gmail.co m
      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. govwrote:
      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(' ', '')
      --


      Comment

      • Paul Hankin

        #4
        Re: Removing Space and &quot;-&quot; from a string

        On May 20, 5:02 pm, "Ahmed, Shakir" <shah...@sfwmd. govwrote:
        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

        Comment

        • Terry Reedy

          #5
          Re: Removing Space and &quot;-&quot; from a string


          "Paul Hankin" <paul.hankin@gm ail.comwrote in message
          news:9d9dadfe-3f91-44a6-8b1b-50fe8003046d@e3 9g2000hsf.googl egroups.com...
          On May 20, 5:02 pm, "Ahmed, Shakir" <shah...@sfwmd. govwrote:
          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.maketra ns('','',' -')
          >>s.translate(t )
          '858215558'




          Comment

          Working...