split

[split]

1. str.split([sep[maxsplit]])

Return a list of the words in the string, using sep as the delimiter string. If maxsplit is given, at most maxsplit splits are done (thus, the list will have at most maxsplit+1 elements). If maxsplit is not specified or -1, then there is no limit on the number of splits (all possible splits are made).

If sep is given, consecutive delimiters are not grouped together and are deemed to delimit empty strings (for example, '1,,2'.split(',') returns ['1', '', '2']). The sep argument may consist of multiple characters (for example,'1<>2<>3'.split('<>') returns ['1', '2', '3']). Splitting an empty string with a specified separator returns [''].

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. Consequently, splitting an empty string or a string consisting of just whitespace with a None separator returns [].

For example, ' 1  2   3  '.split() returns ['1', '2', '3'], and '  1  2   3  '.split(None, 1) returns ['1', '2   3  '].

2. 注意的要点:

  1) str.split() 与str.split(' ') 是不一样的, 不指定sep的会归并連续的空格

  2) 当使用显示sep时, 字符串头的sep和尾部的sep分别会产生一个空字符串项

3. 实例代码:

  

 运行結果:

  

 4. awk 用-F指定sep与不指定sep的我与此类似

原文地址:https://www.cnblogs.com/tekkaman/p/3343184.html