Python中splitlines()方法的使用簡介
更新時間:2015年05月20日 09:34:13 投稿:goldensun
這篇文章主要介紹了Python中splitlines()方法的使用簡介,是Python入門中的基礎知識,需要的朋友可以參考下
splitlines()方法返回一個字符串的所有行,可選包括換行符列表(如果num提供,則為true)
語法
以下是splitlines()方法的語法:
str.splitlines( num=string.count('\n'))
參數(shù)
- num -- 這是任何數(shù),如果存在它會被認為換行需要被包括行數(shù)。
返回值
如果找到匹配的字符串此方法返回true,否則為false。
例子
下面的例子顯示splitlines()方法的使用。
#!/usr/bin/python str = "Line1-a b c d e f\nLine2- a b c\n\nLine4- a b c d"; print str.splitlines( ); print str.splitlines( 0 ); print str.splitlines( 3 ); print str.splitlines( 4 ); print str.splitlines( 5 );
當我們運行上面的程序,它會產(chǎn)生以下結(jié)果:
['Line1-a b c d e f', 'Line2- a b c', '', 'Line4- a b c d'] ['Line1-a b c d e f', 'Line2- a b c', '', 'Line4- a b c d'] ['Line1-a b c d e f\n', 'Line2- a b c\n', '\n', 'Line4- a b c d'] ['Line1-a b c d e f\n', 'Line2- a b c\n', '\n', 'Line4- a b c d'] ['Line1-a b c d e f\n', 'Line2- a b c\n', '\n', 'Line4- a b c d']
相關(guān)文章
keras中模型訓練class_weight,sample_weight區(qū)別說明
這篇文章主要介紹了keras中模型訓練class_weight,sample_weight區(qū)別說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-05-05Python strip lstrip rstrip使用方法
Python中的strip用于去除字符串的首位字符,同理,lstrip用于去除左邊的字符,rstrip用于去除右邊的字符。這三個函數(shù)都可傳入一個參數(shù),指定要去除的首尾字符。2008-09-09