By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,660 Members | 1,100 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,660 IT Pros & Developers. It's quick & easy.

manipulating string question

P: 2
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
Jan 20 '11 #1
Share this Question
Share on Google+
4 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
That can easily be done using string methods and formatting.
Expand|Select|Wrap|Line Numbers
  1. >>> userStr = "999-99-999 or 999-99-998 or 999-99-997"
  2. >>> strList = userStr.split(" or ")
  3. >>> output = " or ".join(["\"APN\" = \'%s\'" % (item) for item in strList])
  4. >>> print output
  5. "APN" = '999-99-999' or "APN" = '999-99-998' or "APN" = '999-99-997'
  6. >>> 
Jan 20 '11 #2

Thekid
100+
P: 145
I know this isn't exactly what you're looking for, but maybe you could build off of this:

Expand|Select|Wrap|Line Numbers
  1. >>>for k in range(999,989,-1):
  2.     print '"APN"=' + "'999-99-%s'" % str(k)
  3.  
  4.  
  5. "APN"='999-99-999'
  6. "APN"='999-99-998'
  7. "APN"='999-99-997'
  8. "APN"='999-99-996'
  9. "APN"='999-99-995'
  10. "APN"='999-99-994'
  11. "APN"='999-99-993'
  12. "APN"='999-99-992'
  13. "APN"='999-99-991'
  14. "APN"='999-99-990'
  15.  
Jan 20 '11 #3

P: 6
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.

Expand|Select|Wrap|Line Numbers
  1. #start code
  2. items = [
  3.     '999-99-999',
  4.     '999-99-999 or 999-99-998',
  5.     '999-99-999 or 999-99-998 or 999-99-997',
  6.     '999-99-999 or 999-99-998 or 999-99-997 or 999-99-996',
  7.     ]
  8.  
  9. seperator = ' or '
  10. prefix = '"APM" = '
  11. item_tags = "'%s'"
  12.  
  13. template = prefix + item_tags
  14. split = lambda item: item.split(seperator)
  15. tag = lambda parsed: [ template % item for item in parsed]
  16. join = lambda tagged: seperator.join( tagged )
  17. reformat = lambda item: join(tag(split(item)))
  18. process = lambda items: [reformat(item) for item in items]
  19.  
  20. def iprocess(items):
  21.     for item in items:
  22.         yield reformat(item)
  23.  
  24. #parsed = split( items[3] )
  25. #tagged = tag( parsed )
  26. #output = join( tagged )
  27. #print join(tag(split(items[3])))
  28. #print reformat(items[3])
  29.  
  30. processed = process(items)
  31. print('"""')
  32. print('Using a simple custom template-like algorithim')
  33. for item in processed: print(item)
  34.  
  35. print('\nUsing above with an iterator') 
  36. for item in iprocess(items): print(item)
  37. print('"""')
  38.  
  39. """
  40. Using a simple custom template-like algorithim
  41. "APM" = '999-99-999'
  42. "APM" = '999-99-999' or "APM" = '999-99-998'
  43. "APM" = '999-99-999' or "APM" = '999-99-998' or "APM" = '999-99-997'
  44. "APM" = '999-99-999' or "APM" = '999-99-998' or "APM" = '999-99-997' or "APM" = '999-99-996'
  45.  
  46. Using above with an iterator
  47. "APM" = '999-99-999'
  48. "APM" = '999-99-999' or "APM" = '999-99-998'
  49. "APM" = '999-99-999' or "APM" = '999-99-998' or "APM" = '999-99-997'
  50. "APM" = '999-99-999' or "APM" = '999-99-998' or "APM" = '999-99-997' or "APM" = '999-99-996'
  51. """
  52. #end code
Jan 20 '11 #4

P: 6
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.
Jan 20 '11 #5

Post your reply

Sign in to post your reply or Sign up for a free account.