Reputation: 393
I am having a stupid problem where there are blank items in the list. There are if statements in it to attempt to offset it, but even with them, the output contains empty parts.
I have been playing around with string.split() a lot and found that what it takes is really specific, and each time i attempt to make it work, it fails. What's going wrong?
a = 'Beautiful, is==; better*than\nugly'
import re
a = re.split(',|\s|=|;|\*|\n| ',a)
for x in a:
if x == '\n':
a.remove(x)
elif x == ' ':
a.remove(x)
elif x == '':
a.remove(x)
print(a)
print("REE")
I want the outcome to only be: ['Beautiful', 'is', 'better', 'than', 'ugly']
But the actual outcome is: ['Beautiful', '', 'is', '', '', '', 'better', 'than', 'ugly']
Upvotes: 0
Views: 356
Reputation: 23089
You want to split on groups of any number of your delimiter characters. The way you're doing it now, you're splitting on only one at a time. This gives you what you want:
import re
a = 'Beautiful, is==; better*than\nugly'
a = re.split(r'[,\s=;*\n]+',a)
print(a)
Result:
['Beautiful', 'is', 'better', 'than', 'ugly']
If you want to split on all non-alphanumeric characters, you can use this expression instead:
a = re.split(r'[^\w]+',a)
Upvotes: 1