Reputation: 109
Say that I have this string:
"hello":"noun":"a greeting";"hello":"verb":"the;action;of;greeting"
How can I make it so string.split(";") or string.split(":") will ignore any characters in quotation marks?
Thanks, PM
Upvotes: 0
Views: 109
Reputation: 18446
Your question doesn't make it 100% clear if all strings are inside quoatation marks. Anyway, this should work. It doesn't remove the quotation marks around the string (you can do this afterwards if you want to).
In [20]: [x for x in re.split(r'(:|;|".*?")', s) if x not in [":",";",""]]
Out[20]:
['',
'"hello"',
'"noun"',
'"a greeting"',
'"hello"',
'"verb"',
'"the;action;of;greeting"']
Upvotes: 0
Reputation: 49320
If you can't get a cleaner input than that, I'd recommend using a regular expression and creating a list
of tuple
s with findall()
:
>>> import re
>>> mystring = '"hello":"noun":"a greeting";"hello":"verb":"the;action;of;greeting"'
>>> result = re.findall(r'"(.+?)":"(.+?)":"(.+?)"', mystring)
>>> for item in result:
... print(*item)
...
hello noun a greeting
hello verb the;action;of;greeting
You can format the output with str.format()
:
>>> for item in result:
... print('{} - {}, {}'.format(*(part.replace(';', ' ') for part in item)))
...
hello - noun, a greeting
hello - verb, the action of greeting
Upvotes: 3