Given a Python string such as:
"good \nand bad and\n\t not great and awesome"
I want to split this into an array on the ands, while also removing the stray \n's and \t's:
["good", "bad", "not great", "awesome"]
How to do so with re.split()?
CodePudding user response:
Here is a regex split approach. We can try splitting on \s and\s , which will target and surrounded by whitespace on both sides. Note that tabs and newlines are whitespace characters and are included by \s.
inp = "good \nand bad and\n\t not great and awesome"
parts = re.split(r'\s and\s ', inp)
print(parts) # ['good', 'bad', 'not great', 'awesome']
CodePudding user response:
You can try this out.
s = "good \nand bad and\n\t not great and awesome"
s = s.replace('\n','').replace('\t','')
s_list = s.split('and')
