Reputation: 69
I am using this code to separate words into a list. The while loop is used to remove any blank spaces that come up, they will be elements with just a ''. The problem is even after I run the while loop there are still elements with just ''. I believe they are due to whitespaces and indentations. The while loop does get rid of about 2/3 of these spaces. Is there a way just to get the words separated? I don't want any blank elements because when I run a loop on them later I get a string index out of range when I reference mylist[i][0]
.
str = fpin.read()
mylist = re.split('[ \n]', str)
i = 0
while(i < len(mylist)):
if mylist[i] == '':
del mylist[i]
i = i + 1
Upvotes: 2
Views: 1801
Reputation: 1033
This is what I did to split a white space separated string into list:
re.split(r'\s*', re.sub(r'^\s+|\s*$', '', input_string))
Another problem with your code is that, you should not use str as a variable name, because str is a built in function.
Upvotes: 0
Reputation: 78670
Unless I'm misunderstanding your specifications, you don't need a regex here. You can just use the string's split
method.
>>> mystr = 'This is \n my awesome \nstring'
>>> mystr.split()
['This', 'is', 'my', 'awesome', 'string']
Upvotes: 6