string splitting

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • rdharles@gmail.com

    #1

    string splitting

    Hello,
    I have thousands of files that look something like this:

    wisconsin_state .txt
    french_guiana_d istrict.txt
    central_african _republic_provi nce.txt

    I need to extract the string between the *last* underscore and the
    extention.
    So based on the files above, I want returned:
    state
    district
    province

    My plan was to use .split or .find but I can't figure out how locate
    only the last underscore in the filename.

    Anyone have any ideas?

    Thanks.
    R.D.

  • hiaips

    #2
    Re: string splitting


    rdhar...@gmail. com wrote:
    Hello,
    I have thousands of files that look something like this:
    >
    wisconsin_state .txt
    french_guiana_d istrict.txt
    central_african _republic_provi nce.txt
    >
    I need to extract the string between the *last* underscore and the
    extention.
    So based on the files above, I want returned:
    state
    district
    province
    >
    My plan was to use .split or .find but I can't figure out how locate
    only the last underscore in the filename.
    >
    Anyone have any ideas?
    >
    Thanks.
    R.D.
    Hi,

    Try splitting the string on "." and using rfind to find the last
    instance of "_".

    i.e.,
    myStr = "wisconsin_stat e.txt"
    pieces = myStr.split("." )
    substr = pieces[0][pieces[0].rfind("_") + 1:]

    --hiaips

    Comment

    • Simon Brunning

      #3
      Re: string splitting

      On 16 Oct 2006 12:12:38 -0700, rdharles@gmail. com <rdharles@gmail .comwrote:
      Hello,
      I have thousands of files that look something like this:
      >
      wisconsin_state .txt
      french_guiana_d istrict.txt
      central_african _republic_provi nce.txt
      >
      I need to extract the string between the *last* underscore and the
      extention.
      So based on the files above, I want returned:
      state
      district
      province
      >
      My plan was to use .split or .find but I can't figure out how locate
      only the last underscore in the filename.
      >>spam = 'central_africa n_republic_prov ince.txt'
      >>spam.split('. ')[0].rsplit('_', 1)[-1]
      'province'

      --
      Cheers,
      Simon B
      simon@brunningo nline.net

      Comment

      • rdharles@gmail.com

        #4
        Re: string splitting

        Much thanks for your replies hiaips & Simon!
        R.D.

        Comment

        • bearophileHUGS@lycos.com

          #5
          Re: string splitting

          A pair of solutions:
          >>s = "central_africa n_republic_prov ince.txt"
          >>s.rsplit("_ ", 1)[-1].split(".")[0]
          'province'
          >>import re
          >>p = re.compile(r"_ ([^_]+) \.", re.VERBOSE)
          >>s = """\
          .... wisconsin_state .txt
          .... french_guiana_d istrict.txt
          .... central_african _republic_provi nce.txt"""
          >>p.findall(s )
          ['state', 'district', 'province']

          Bye,
          bearophile

          Comment

          • stefaan

            #6
            Re: string splitting

            Anyone have any ideas?

            l = "wisconsin_stat e.txt"
            l.split(".")[0].split("_")[-1]

            Explanation:
            -------------------
            the split(".")[0] part takes everything before the "."

            the split("_")[-1] part selects in the last element in the list of
            substrings which are separated by "_"

            Comment

            • George Sakkis

              #7
              Re: string splitting

              rdharles@gmail. com wrote:
              Hello,
              I have thousands of files that look something like this:
              >
              wisconsin_state .txt
              french_guiana_d istrict.txt
              central_african _republic_provi nce.txt
              >
              I need to extract the string between the *last* underscore and the
              extention.
              So based on the files above, I want returned:
              state
              district
              province
              def extract(s):
              return s[s.rfind('_')+1: s.rfind('.')]


              George

              Comment

              Working...