Problem splitting a string
Robert Kern
robert.kern at gmail.com
Sat Oct 15 01:03:08 EDT 2005
Anthony Liu wrote:
> I have this simple string:
>
> mystr = 'this_NP is_VL funny_JJ'
>
> I want to split it and give me a list as
>
> ['this', 'NP', 'is', 'VL', 'funny', 'JJ']
> I think the documentation does say that the
> separator/delimiter can be a string representing all
> delimiters we want to use.
No, it doesn't.
In [1]: str.split?
Type: method_descriptor
Base Class: <type 'method_descriptor'>
String Form: <method 'split' of 'str' objects>
Namespace: Python builtin
Docstring:
S.split([sep [,maxsplit]]) -> list of strings
Return a list of the words in the string S, using sep as the
delimiter string. If maxsplit is given, at most maxsplit
splits are done. If sep is not specified or is None, any
whitespace string is a separator.
> I do I split the string by using both ' ' and '_' as
> the delimiters at once?
You could use regular expressions as Jason Stitt mentions, or you could
replace '_' with ' ' and then split.
In [2]: mystr = 'this_NP is_VL funny_JJ'
In [3]: mystr.replace('_', ' ').split()
Out[3]: ['this', 'NP', 'is', 'VL', 'funny', 'JJ']
--
Robert Kern
rkern at ucsd.edu
"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
More information about the Python-list
mailing list