python - using re.split() to separate a string into list -
i using code separate words list. while loop used remove blank spaces come up, elements ''. problem after run while loop there still elements ''. believe due whitespaces , indentations. while loop rid of 2/3 of these spaces. there way words separated? don't want blank elements because when run loop on them later string index out of range when reference mylist[i][0]
.
str = fpin.read() mylist = re.split('[ \n]', str) = 0 while(i < len(mylist)): if mylist[i] == '': del mylist[i] = + 1
unless i'm misunderstanding specifications, don't need regex here. can use string's split
method.
>>> mystr = 'this \n awesome \nstring' >>> mystr.split() ['this', 'is', 'my', 'awesome', 'string']
Comments
Post a Comment