Maxon Developers Maxon Developers
    • Documentation
      • Cinema 4D Python API
      • Cinema 4D C++ API
      • Cineware API
      • ZBrush Python API
      • ZBrush GoZ API
      • Code Examples on Github
    • Forum
    • Downloads
    • Support
      • Support Procedures
      • Registered Developer Program
      • Plugin IDs
      • Contact Us
    • Categories
      • Overview
      • News & Information
      • Cinema 4D SDK Support
      • Cineware SDK Support
      • ZBrush 4D SDK Support
      • Bugs
      • General Talk
    • Unread
    • Recent
    • Tags
    • Users
    • Login

    split String at first letter

    Scheduled Pinned Locked Moved PYTHON Development
    7 Posts 0 Posters 662 Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • H Offline
      Helper
      last edited by

      On 09/03/2017 at 22:23, xxxxxxxx wrote:

      Hi there,

      I want to split a String like "0123_456-Name_1438" and get the "Name_1438 ".
      My tryout was this:

      s = "0123_456-Name_1438"
      for c in s:
                  if int(c) :
                      print c+" = ",int(c)," ",type(c)
                  else:
                      print c+" = ",type(c)
      

      ...a simple try if typecasting can be used to differenciate letters and numbers in a string.

      The console tells: "ValueError: invalid literal for int() with base 10: 'N'"
      Do you have an idea how that can be solved in python?

      edit: I found str.isdigit() that does the trick 😉

      1 Reply Last reply Reply Quote 0
      • H Offline
        Helper
        last edited by

        On 10/03/2017 at 01:23, xxxxxxxx wrote:

        Ok. that's how I solved it:

          
        def stripLNumbers(s) :
            if s != False:
                num = -1
                for c in s:
                    num = num + 1
                    if c.isalpha() and c != "-" and c != "_":    #Is there a more elegant way to exclude special characters?
                        return s[num:]
        
        1 Reply Last reply Reply Quote 0
        • H Offline
          Helper
          last edited by

          On 10/03/2017 at 06:36, xxxxxxxx wrote:

          So basicly you want all after "-"

          If yes simply do

              a = "0123_456-Name_1438"
              split = a.split("-")
              value = str()
              for string in split[1:]:
                  value += string
                  
              print value
          

          But if you jsut want the text look about(google it) regex and re module in python 😉

          1 Reply Last reply Reply Quote 0
          • H Offline
            Helper
            last edited by

            On 10/03/2017 at 07:19, xxxxxxxx wrote:

            Couldn't it be done with a single split call?

            a = "0123_456-Name_1438"
            prefix, name = a.split("-", 1)
            
            1 Reply Last reply Reply Quote 0
            • H Offline
              Helper
              last edited by

              On 10/03/2017 at 07:23, xxxxxxxx wrote:

              Hahaha completly true ! Didn't know split can take two arguments...

              But mine can take also one line 😛

              a = "0123_456-Name_1438"
              value = str().join(a.split("-")[1:])
                  
              print value
              
              1 Reply Last reply Reply Quote 0
              • H Offline
                Helper
                last edited by

                On 10/03/2017 at 10:51, xxxxxxxx wrote:

                The condition is "at the first letter", not "after the first hyphen" 😉
                Regex is probably the most simple solution.

                import re
                def strip_numbers(s) :
                  return re.search('[A-Za-z].*', s).group()

                Or in plain python (very similar to gmak's solution)

                def strip_numbers(s) :
                  for i, c in enumerate(s) :
                    if c.isalpha() :
                      return s[i:]
                  raise ValueError('no alphanumerical character in string {!r}'.format(s))

                Cheers,
                -Niklas

                1 Reply Last reply Reply Quote 0
                • H Offline
                  Helper
                  last edited by

                  On 13/03/2017 at 06:21, xxxxxxxx wrote:

                  THX!
                  Nice one, NiklasR!

                  1 Reply Last reply Reply Quote 0
                  • First post
                    Last post