1

Possible Duplicate:
Split string on whitespace in python

I have a string like so:

['.text      0x1000       0xb51b       0xb600       6.259216    ']

and I would like to split it into this:

[.text, 0x1000, 0xb51b... etc]

So far I've tried: re.split("( )", b) and re.split("[ \t]", b)

but to no avail. I get things like:

.['.text', ' ', '0x1000', ' ', '0xb51b', ' ', '0xb600', ' ', '6.259216', ' ', '']

or some others with even more whitespaces. I know I could just remove the whitespaces from the string but I would much rather just straight up use a RE to split them in the first place.

0

3 Answers 3

7

Why not just use the regular str.split?

'.text      0x1000       0xb51b       0xb600       6.259216    '.split()

To quote the documentation:

if sep is not specified or is None, a different splitting algorithm is applied: runs of consecutive whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the string has leading or trailing whitespace.


As an aside, I've found that using mystring.split(None) (as opposed to mystring.split()) is sometimes quite a useful thing to keep in mind as it allows you to not "hardcode" the splitting algorithm.

Sign up to request clarification or add additional context in comments.

3 Comments

Because the amount of spaces vary
@Shelby.S -- That doesn't matter. -- Give it a try :-)
.split() would take all whitespaces into account. This would work
2

Try this:

import re
re.split("\s*", yourInputString.strip()) # result as List

Comments

1

Another option is to firstly clear the multiple whitespaces, and replace them with single white spaces. After that you you do a split(" ") with one whitespace:

re.sub(r"  +"," ", text).split(" ")

4 Comments

Why would you ever use this if you can do it all in 1 go with str.split? I suppose you if you wanted to make sure that you didn't split on "\t" or something ...
Of course, in this case, it's easier to just use re.split(' +',string_to_split)
I agree with you. For the specific purpose as in this question, the str.split is most appropriate. But if you have some other multicharacter occurences, then my solution is maybe more general.
You're right... Totally forgot about re.split .

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.