Use the str.split method:
>>> "MATCHES__STRING".split("__")
['MATCHES', 'STRING']
Answer from adamk on Stack OverflowTrying to understand string split()
Split a string by a delimiter in Python - Stack Overflow
python - How do I split a string into a list of characters? - Stack Overflow
How to split each string into words and collects all words in a new list.
Hi,
I am unable to wrap my head around how string split() works in Python. Consider the following examples:
β/β.split(β/β) # Outputs [ββ, ββ]
β a b β.split() # Outputs [βaβ, βbβ]
I was expecting the second example to return [ββ, βaβ, βbβ, ββ]. Why does split() omit the empty strings in the output list?
Use the str.split method:
>>> "MATCHES__STRING".split("__")
['MATCHES', 'STRING']
Besides split and rsplit, there is partition/rpartition. It separates string once, but the way question was asked, it may apply as well.
Example:
>>> "MATCHES__STRING".partition("__")
('MATCHES', '__', 'STRING')
>>> "MATCHES__STRING".partition("__")[::2]
('MATCHES', 'STRING')
And a bit faster then split("_",1):
$ python -m timeit "'validate_field_name'.split('_', 1)[-1]"
2000000 loops, best of 5: 136 nsec per loop
$ python -m timeit "'validate_field_name'.partition('_')[-1]"
2000000 loops, best of 5: 108 nsec per loop
Timeit lines are based on this answer
Use the list constructor:
>>> list("foobar")
['f', 'o', 'o', 'b', 'a', 'r']
list builds a new list using items obtained by iterating over the input iterable. A string is an iterable -- iterating over it yields a single character at each iteration step.
You take the string and pass it to list()
s = "mystring"
l = list(s)
print l
I'm working on a book I've downloaded from project Guttenberg and after doing some data cleaning I have ended up with a list of strings. Next I should split each string into words and collect all the words in a new list.
I have tried to use the split commando but somehow I end up with transforming each string into a list instead of a a string.
I'm sorry if I'm being vague. Please let me know if I should provide more infromation...
I have created the following function.
def splitter():
wordList = [s.split(" ") for s in book]
return wordList
book = splitter()
Somehow I end up with the following output when I type book[0] into the console:
['the',
'history',
'of',
'australian',
'exploration',
'from',
'1788',
'to',
'1888']
Instead of splitting the string and ending up with a list of strings I have created a list of lists.
What I want to end up with is:
In[] book[0]
Out[] the
and
in[] book[:9]
['the',
'history',
'of',
'australian',
'exploration',
'from',
'1788',
'to',
'1888']
re.split()
re.split(pattern, string[, maxsplit=0])
Split string by the occurrences of pattern. If capturing parentheses are used in pattern, then the text of all groups in the pattern are also returned as part of the resulting list. If maxsplit is nonzero, at most maxsplit splits occur, and the remainder of the string is returned as the final element of the list. (Incompatibility note: in the original Python 1.5 release, maxsplit was ignored. This has been fixed in later releases.)
>>> re.split('\W+', 'Words, words, words.')
['Words', 'words', 'words', '']
>>> re.split('(\W+)', 'Words, words, words.')
['Words', ', ', 'words', ', ', 'words', '.', '']
>>> re.split('\W+', 'Words, words, words.', 1)
['Words', 'words, words.']
A case where regular expressions are justified:
import re
DATA = "Hey, you - what are you doing here!?"
print re.findall(r"[\w']+", DATA)
# Prints ['Hey', 'you', 'what', 'are', 'you', 'doing', 'here']
Luckily, Python has this built-in :)
import re
# Regex pattern splits on substrings "; " and ", "
re.split('; |, ', string_to_split)
Update:
Following your comment:
>>> string_to_split = 'Beautiful, is; better*than\nugly'
>>> import re
>>> re.split('; |, |\*|\n', string_to_split)
['Beautiful', 'is', 'better', 'than', 'ugly']
Do a str.replace('; ', ', ') and then a str.split(', ')