Is it possible to have a split() with two or more conditions in Python?

Asked

Viewed 812 times

1

Good afternoon staff, developing a code, I came across a question: I would like to realize a split() with n conditions? Or any efficient way to do?

For example:

I am requesting for a user to enter some information through a input(), but I would like to carry out the split() so much if he uses , or . how much also to use / or -.

I know it doesn’t work, since the documentation is presenting this hierarchy str.split(sep=None, maxsplit=-1), I was thinking of something functional that looked like:

information_list = list(input('Enter the informations: ').split(',', '.', '/', '-'))

As a return, I wish to have a list of the values that the user typed separated by the 'arguments' I mentioned above. However, if you have other methods to indicate in order to accomplish something similar, it would also be of great value.

1 answer

7


You can use the regex.split() that uses a regular expression to mount the separator. For example:

import re
a = "a,b.c|d"
re.split("[,\.|-]",a)

That will return the list:

['a', 'b', 'c', 'd']
  • 1

    Thank you for your attention Giovanni.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.