- 论坛徽章:
- 0
|
如果你只是想要获得非空白内容,如下方式即可:- >>> line = "a 1 b 2 c 3 d 4";
- >>> splited = line.split();
- >>> print splited
- ['a', '1', 'b', '2', 'c', '3', 'd', '4']
复制代码 其中,不指定split的分隔符,则会使用默认的方式,去掉所有的空白类字符。
相关语法:
string.split(s[, sep[, maxsplit]])
Return a list of the words of the string s. If the optional second argument sep is absent or None, the words are separated by arbitrary strings of whitespace characters (space, tab, newline, return, formfeed). If the second argument sep is present and not None, it specifies a string to be used as the word separator. The returned list will then have one more item than the number of non-overlapping occurrences of the separator in the string. The optional third argument maxsplit defaults to 0. If it is nonzero, at most maxsplit number of splits occur, and the remainder of the string is returned as the final element of the list (thus, the list will have at most maxsplit+1 elements).
The behavior of split on an empty string depends on the value of sep. If sep is not specified, or specified as None, the result will be an empty list. If sep is specified as any string, the result will be a list containing one element which is an empty string. |
|