String Manipulation

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • lamar_air

    String Manipulation

    I need a piece of code that takes a string like this string1 =
    "aaa/bbb/ccc/dd" and extracts a string containting the character after
    the last "/"

    So for this example the result would be "dd"

    like this:
    for i=0; string1.right(i ) != '/'; i++

    result = string1.mid(i, string1.length( ))

    but in python.
  • Cousin Stanley

    #2
    Re: String Manipulation

    | I need a piece of code that takes a string like this
    | string1 = "aaa/bbb/ccc/dd" and extracts a string containting
    | the character after the last "/"
    |
    | So for this example the result would be "dd"
    | ...

    lamar_air ...

    Here is one way ...

    [color=blue][color=green][color=darkred]
    >>> str_in = 'aaa/bbb/ccc/dd'
    >>>
    >>> list_in = str_in.split( '/' )
    >>>
    >>> last_element = list_in[ -1 ]
    >>>
    >>> print last_element[/color][/color][/color]
    dd[color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]


    --
    Cousin Stanley
    Human Being
    Phoenix, Arizona


    Comment

    • Irmen de Jong

      #3
      Re: String Manipulation

      John Hunter wrote:
      [color=blue][color=green][color=darkred]
      >>>>>>"lamar" == lamar air <lamar_air@hotm ail.com> writes:[/color][/color]
      >
      >
      > lamar> I need a piece of code that takes a string like this
      > lamar> string1 = "aaa/bbb/ccc/dd" and extracts a string
      > lamar> containting the character after the last "/"
      >
      > One good way to do this is to split the string on the '/' character,
      > which creates a list of strings
      >[color=green][color=darkred]
      > >>> string1 = "aaa/bbb/ccc/dd"
      > >>> parts = string1.split('/')
      > >>> parts[/color][/color]
      > ['aaa', 'bbb', 'ccc', 'dd']
      >
      > Now you just want to get the last element of this list; python allows
      > you to index with -1 to get the last element, -2 to get the second to
      > last, etc...
      >[color=green][color=darkred]
      > >>> parts[-1][/color][/color]
      > 'dd'
      >
      > JDH
      >[/color]

      While is is perfectly acceptable, you might want to consider another
      solution if it is *path names* you are manipulating:
      [color=blue][color=green][color=darkred]
      >>> import os
      >>> os.path.split(" aaa/bbb/ccc/dd")[/color][/color][/color]
      ('aaa/bbb/ccc', 'dd')[color=blue][color=green][color=darkred]
      >>> os.path.split(" aaa/bbb/ccc/dd")[1][/color][/color][/color]
      'dd'


      because this will work correctly on other platforms too when the
      path separator is not '/'.

      --Irmen de JOng

      Comment

      • Brandon Beck

        #4
        Re: String Manipulation

        I know a few people replied to this already, but here's one additional
        possibility. If the data in string1 is a file path for your platform,
        then you can use the os.path module.
        [color=blue][color=green][color=darkred]
        >>> import os.path
        >>> os.path.split(" aaa/bbb/ccc/dd")[/color][/color][/color]
        ('aaa/bbb/cc', 'dd')[color=blue][color=green][color=darkred]
        >>> os.path.splitex t("filename.ext ")[/color][/color][/color]
        ('filename', '.ext')

        The other suggestions will of course work, but if you data is a file
        path, then using the os.path module should be a more portable solution.

        Brandon



        "lamar_air" <lamar_air@hotm ail.com> wrote in message
        news:2c6431ab.0 307151223.4173c [email protected] gle.com...[color=blue]
        > I need a piece of code that takes a string like this string1 =
        > "aaa/bbb/ccc/dd" and extracts a string containting the character after
        > the last "/"
        >
        > So for this example the result would be "dd"
        >
        > like this:
        > for i=0; string1.right(i ) != '/'; i++
        >
        > result = string1.mid(i, string1.length( ))
        >
        > but in python.[/color]


        Comment

        • Inyeol Lee

          #5
          Re: String Manipulation

          On Tue, Jul 15, 2003 at 01:58:09PM -0700, Cousin Stanley wrote:[color=blue]
          > | I need a piece of code that takes a string like this
          > | string1 = "aaa/bbb/ccc/dd" and extracts a string containting
          > | the character after the last "/"
          > |
          > | So for this example the result would be "dd"
          > | ...
          >
          > lamar_air ...
          >
          > Here is one way ...
          >
          >[color=green][color=darkred]
          > >>> str_in = 'aaa/bbb/ccc/dd'
          > >>>
          > >>> list_in = str_in.split( '/' )
          > >>>
          > >>> last_element = list_in[ -1 ]
          > >>>
          > >>> print last_element[/color][/color]
          > dd[color=green][color=darkred]
          > >>>[/color][/color]
          >
          >
          > --
          > Cousin Stanley
          > Human Being
          > Phoenix, Arizona
          >[/color]

          In Unix,
          [color=blue][color=green][color=darkred]
          >>> os.path.basenam e("aaa/bbb/ccc")[/color][/color][/color]
          'ccc'

          Inyeol

          Comment

          • Bengt Richter

            #6
            Re: String Manipulation

            On 15 Jul 2003 13:23:09 -0700, lamar_air@hotma il.com (lamar_air) wrote:
            [color=blue]
            >I need a piece of code that takes a string like this string1 =
            >"aaa/bbb/ccc/dd" and extracts a string containting the character after
            >the last "/"
            >
            >So for this example the result would be "dd"
            >
            >like this:
            >for i=0; string1.right(i ) != '/'; i++
            >
            >result = string1.mid(i, string1.length( ))
            >
            >but in python.[/color]

            Others have posted the split() solutions.
            You could also search backward in Python:
            [color=blue][color=green][color=darkred]
            >>> s = "aaa/bbb/ccc/dd"
            >>> s[s.rfind('/')+1:][/color][/color][/color]
            'dd'[color=blue][color=green][color=darkred]
            >>> s='no_slashes'
            >>> s[s.rfind('/')+1:][/color][/color][/color]
            'no_slashes'

            Regards,
            Bengt Richter

            Comment

            • Bengt Richter

              #7
              Re: String Manipulation

              On 15 Jul 2003 19:51:56 -0700, [email protected] m.ernet.in (Vinoo Vasudevan) wrote:
              [color=blue]
              >lamar_air@hotm ail.com (lamar_air) wrote in message news:<2c6431ab. 0307151223.4173 [email protected] ogle.com>...[color=green]
              >> I need a piece of code that takes a string like this string1 =
              >> "aaa/bbb/ccc/dd" and extracts a string containting the character after
              >> the last "/"
              >>
              >> So for this example the result would be "dd"
              >>
              >> like this:
              >> for i=0; string1.right(i ) != '/'; i++
              >>
              >> result = string1.mid(i, string1.length( ))
              >>
              >> but in python.[/color]
              >
              >How about this:
              >string1 = 'aaa/bbb/ccc/dd'
              >result = string1[string1.rfind('/')+1:]
              >
              >Hope it's helpful,[/color]
              Sorry Vinoo, for some reason your post did not show up for me before I posted
              the same solution, even though your post is dated much before mine.
              I guess it has to do with delays in news servers forwarding and such.

              Regards,
              Bengt Richter

              Comment

              Working...