code_junkie

Splitting arguments — preserving quoted substrings — in python [closed]

Nov 14th, 2011
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.33 KB | None | 0 0
  1. def splitstring(string):
  2. """
  3. >>> string = 'apple orange "banana tree" green'
  4. >>> splitstring(string)
  5. ['apple', 'orange', 'green', '"banana tree"']
  6. """
  7. import re
  8. p = re.compile(r'"[w ]+"')
  9. quoted_item = p.search(string).group()
  10. newstring = p.sub('', string)
  11. return newstring.split() + [quoted_item]
Add Comment
Please, Sign In to add comment