程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Different usages of three Python split functions (split, rsplit, splitlines)

編輯:Python

List of articles

  • split()
    • split Four splitting rules
  • rsplit()
  • splitlines()
    • splitlines And split The difference between
  • Reference material


split()

split() Function basic format :

split(sep=None, maxsplit=-1)
Returns a list of words in a string , Use sep As a separate string .
If given maxsplit, At most maxsplit Sub resolution ( therefore , The list will have at most maxsplit+1 Elements ).
If maxsplit Not specified or is -1, The number of splits is not limited ( Make all possible splits ).

seq The delimited string defaults to None,maxsplit The default indicates that the number of splits is not limited .
split() Return to one list

split Four splitting rules

situation 1: to `sep``: basis sep Split all strings

'a.bc'.split('.')
['a', 'bc']

situation 2: to sep, to maxsplit: basis sep For strings, follow maxsplit Value of to split

'a.b.c'.split('.',maxsplit=1)
['a', 'b.c']

situation 3: If sep Not specified or is None Consecutive spaces are treated as a single separator , The result will not contain an empty string at the beginning or end , If the string contains prefix or suffix spaces .

'1 2 3'.split()
#['1', '2', '3']
'1 2 3'.split(maxsplit=1)
#['1', '2 3']
' 1 2 3 '.split()
#['1', '2', '3']

situation 4: Handle consecutive delimiters : Consecutive delimiters are not grouped together but are treated as Separate empty strings

'a.b..c'.split('.')
['a', 'b', '', 'c']

rsplit()

rsplit() The basic format :

rsplit(sep=None, maxsplit=- 1)
Returns a list of words in a string , Use sep As a separate string .
If given maxsplit, At most maxsplit Sub resolution , from Far right Start .

And split() function The difference between :rsplit() from Far right Start splitting
rsplit() The rest of the actions and split() identical

splitlines()

splitlines() The basic format :

splitlines(keepends=False)
Returns a list of lines in the original string , Split at the line boundary .
The result list does not contain row boundaries , Unless you give keepends And it's true .

situation 1: Default splitlines()

'ab c\n\nde fg\rkl\r\n'.splitlines()
#['ab c', '', 'de fg', 'kl']

situation 2: Include row boundaries keepends = True

'ab c\n\nde fg\rkl\r\n'.splitlines(keepends=True)
#['ab c\n', '\n', 'de fg\r', 'kl\r\n']

splitlines And split The difference between

When a delimited string is given sep when , For an empty string, this method returns an empty list ; A newline at the end does not add extra lines to the result .

"".splitlines()
#[]
"One line\n".splitlines()
#['One line']

As a comparison ,split('\n') As the result of the :

''.split('\n')
#['']
'Two lines\n'.split('\n')
#['Two lines', '']

Reference material

Built in type -split:https://docs.python.org/zh-cn/3/library/stdtypes.html?highlight=split


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved