manipulating string question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jpyguy
    New Member
    • Jan 2011
    • 2

    manipulating string question

    I'm really new to python so I'm hoping for some serious help.

    I have an application that is taking in users inputs from a text box. I need to create a python script that takes in this user sting and formats it.

    The user string will look like the following examples:

    999-99-999
    999-99-999 or 999-99-998
    999-99-999 or 999-99-998 or 999-99-997
    999-99-999 or 999-99-998 or 999-99-997 or 999-99-996
    and so on there could be more values separator by the or operator

    So basically I would like to develop a python script that changes the value format to something like:

    "APN" = '999-99-999'
    "APN" = '999-99-999' or "APN" = '999-99-998'
    "APN" = '999-99-999' or "APN" = '999-99-998' or "APN" = '999-99-997'
    "APN" = '999-99-999' or "APN" = '999-99-998' or "APN" = '999-99-997' or "APN" = '999-99-996'
    and so on

    WOuld someone be kind enough to tell me how I can accomplish this.

    Thanks
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    That can easily be done using string methods and formatting.
    Code:
    >>> userStr = "999-99-999 or 999-99-998 or 999-99-997"
    >>> strList = userStr.split(" or ")
    >>> output = " or ".join(["\"APN\" = \'%s\'" % (item) for item in strList])
    >>> print output
    "APN" = '999-99-999' or "APN" = '999-99-998' or "APN" = '999-99-997'
    >>>

    Comment

    • Thekid
      New Member
      • Feb 2007
      • 145

      #3
      I know this isn't exactly what you're looking for, but maybe you could build off of this:

      Code:
      >>>for k in range(999,989,-1):
      	print '"APN"=' + "'999-99-%s'" % str(k)
      
      	
      "APN"='999-99-999'
      "APN"='999-99-998'
      "APN"='999-99-997'
      "APN"='999-99-996'
      "APN"='999-99-995'
      "APN"='999-99-994'
      "APN"='999-99-993'
      "APN"='999-99-992'
      "APN"='999-99-991'
      "APN"='999-99-990'

      Comment

      • Dev Player
        New Member
        • Jan 2011
        • 6

        #4
        I really like bvdet's solution, succinct and Pythonic. Make it in a function and you are good to go.

        It's a little cryptic for some beginners as list comprehensions are not often a well practiced looping tool. All those string escapes can camoflage its elegance.

        And the join() function is backwards looking to beginners too as it looks like this:
        seperator.join( sequence) instead of
        join(sequence, seperator)

        Following I broke his recipe down into parts so you can learn to rebuild his solution. I think you could have easily figured out how to program a recipe, if you knew how to break your problem down.

        As bvdet's recipe shows, the trick to your solution is to break the string apart on the seperator " or " (with spaces included there). The second trick is knowing about how to rebuild strings in Python. There are many tools to parse and build strings. Knowing these leads you to your solutions. Look at the strings library for old ways to do it. Look at str() methods for some easy ways too.

        The two Python tools (s)he combines are:
        1. Python format strings: "%s" % (item,)
        2. and str.join() (do a dir(str) and then help(str.join))

        An aside regarding your original question about user input; Look into validators. Validators are designed to work with "input". Personally I've never used them.

        The not-so-Pythonic solution I give below is intended to give you some other tools that might help. Sometimes it's best to see solutions to real problems one has to see the benefits. Some tools I use below are lambda, a one line unnamed function definition, yield statement in a function to make an iterator (instead of the return statement), simpler looking list comprehensions (a fast for loop).

        Also I tried to break it down and use terms which can lead you to learn templating. As user various user input might have different formats I try to show an home-grown starter "template". Templates are handy if you have lots of different formats to deal with.

        The specific data you gave may be the only format you need to convert but others with a similar question my need something more adaptable.

        I did this code in PySlicesShell; Google it.

        Code:
        #start code
        items = [
            '999-99-999',
            '999-99-999 or 999-99-998',
            '999-99-999 or 999-99-998 or 999-99-997',
            '999-99-999 or 999-99-998 or 999-99-997 or 999-99-996',
            ]
        
        seperator = ' or '
        prefix = '"APM" = '
        item_tags = "'%s'"
        
        template = prefix + item_tags
        split = lambda item: item.split(seperator)
        tag = lambda parsed: [ template % item for item in parsed]
        join = lambda tagged: seperator.join( tagged )
        reformat = lambda item: join(tag(split(item)))
        process = lambda items: [reformat(item) for item in items]
        
        def iprocess(items):
            for item in items:
                yield reformat(item)
        
        #parsed = split( items[3] )
        #tagged = tag( parsed )
        #output = join( tagged )
        #print join(tag(split(items[3])))
        #print reformat(items[3])
        
        processed = process(items)
        print('"""')
        print('Using a simple custom template-like algorithim')
        for item in processed: print(item)
        
        print('\nUsing above with an iterator') 
        for item in iprocess(items): print(item)
        print('"""')
        
        """
        Using a simple custom template-like algorithim
        "APM" = '999-99-999'
        "APM" = '999-99-999' or "APM" = '999-99-998'
        "APM" = '999-99-999' or "APM" = '999-99-998' or "APM" = '999-99-997'
        "APM" = '999-99-999' or "APM" = '999-99-998' or "APM" = '999-99-997' or "APM" = '999-99-996'
        
        Using above with an iterator
        "APM" = '999-99-999'
        "APM" = '999-99-999' or "APM" = '999-99-998'
        "APM" = '999-99-999' or "APM" = '999-99-998' or "APM" = '999-99-997'
        "APM" = '999-99-999' or "APM" = '999-99-998' or "APM" = '999-99-997' or "APM" = '999-99-996'
        """
        #end code

        Comment

        • Dev Player
          New Member
          • Jan 2011
          • 6

          #5
          Another wide-spread used string manipulation tool, although not really suited to your issue here is the re module, which refers to regex.

          Some feel regex is clunky and learning regex is not so simple for some. But regex is used in many other programming areas outside of Python.

          import re
          dir(re)
          help(re)
          help(re.findall
          )

          So not for this post, but it'll help round out one's skills with string manipulation.

          Comment

          Working...