Skip to content Skip to sidebar Skip to footer

Python Complicated Splitting Of A String

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 charac

Solution 1:

If you can't get a cleaner input than that, I'd recommend using a regular expression and creating a list of tuples 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

Solution 2:

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 notin [":",";",""]]
Out[20]:
['',
 '"hello"',
 '"noun"',
 '"a greeting"',
 '"hello"',
 '"verb"',
 '"the;action;of;greeting"']

Post a Comment for "Python Complicated Splitting Of A String"