CosmicRabbitMediaInc
CosmicRabbitMediaInc

Reputation: 1175

Split string without non-characters

I'm trying to split a string that looks like this for example:

':foo [bar]'

Using str.split() on this of course returns [':foo','[bar]']

But how can I make it return just ['foo','bar'] containing only these characters?

Upvotes: 4

Views: 8914

Answers (4)

lvc
lvc

Reputation: 35079

Do str.split() as normal, and then parse each element to remove the non-letters. Something like:

>>> my_string = ':foo [bar]'
>>> parts = [''.join(c for c in s if c.isalpha()) for s in my_string.split()]
['foo', 'bar']

Upvotes: 1

DSM
DSM

Reputation: 353119

I don't like regular expressions, but do like Python, so I'd probably write this as

>>> s = ':foo [bar]'
>>> ''.join(c for c in s if c.isalnum() or c.isspace())
'foo bar'
>>> ''.join(c for c in s if c.isalnum() or c.isspace()).split()
['foo', 'bar']

The ''.join idiom is a little strange, I admit, but you can almost read the rest in English: "join every character for the characters in s if the character is alphanumeric or the character is whitespace, and then split that".

Alternatively, if you know that the symbols you want to remove will always be on the outside and the word will still be separated by spaces, and you know what they are, you might try something like

>>> s = ':foo [bar]'
>>> s.split()
[':foo', '[bar]']
>>> [word.strip(':[]') for word in s.split()]
['foo', 'bar']

Upvotes: 12

RanRag
RanRag

Reputation: 49567

You have to try regular expressions.

Use re.sub() to replace :,[,] characters and than split your resultant string with white space as delimiter.

>>> st = ':foo [bar]'
>>> import re
>>> new_st = re.sub(r'[\[\]:]','',st)
>>> new_st.split(' ')
['foo', 'bar']

Upvotes: 0

tpg2114
tpg2114

Reputation: 15100

You'll have to pass through the list ['foo','[bar]'] and strip out all non-letter characters, using regular expressions. Check Regex replace (in Python) - a simpler way? for examples and references to documentation.

Upvotes: 0

Related Questions