码迷,mamicode.com
首页 > 编程语言 > 详细

python split的使用

时间:2014-09-26 20:54:48      阅读:236      评论:0      收藏:0      [点我收藏+]

标签:python split

split是分隔字符的(貌似类似于cut)

>>> a= ‘192.168.1.60 root 22 111111‘

>>> a

‘192.168.1.60 root 22 111111‘

>>> a.split()默认以空格为分隔

[‘192.168.1.60‘, ‘root‘, ‘22‘, ‘111111‘]

>>> b=‘my..name..is..ghn‘

>>> b

‘my..name..is..ghn‘

>>> b.split()

[‘my..name..is..ghn‘]

>>> b.split(..)

  File "<stdin>", line 1

    b.split(..)

            ^

SyntaxError: invalid syntax

>>> b.split(‘..‘)已..为分隔

[‘my‘, ‘name‘, ‘is‘, ‘ghn‘]

>>> b.split(‘..‘,1)

[‘my‘, ‘name..is..ghn‘]

>>> b.split(‘..‘,2)

[‘my‘, ‘name‘, ‘is..ghn‘]

>>> b.split(‘..‘,0)

[‘my..name..is..ghn‘]

>>> b.split(‘..‘,-1)

[‘my‘, ‘name‘, ‘is‘, ‘ghn‘]

>>> b.split(‘..‘)[0] 截取某一个特定的字段,类似与cut-d:-f1

‘my‘

>>> b.split(‘..‘)[1]

‘name‘

>>> b.split(‘..‘)[2]

‘is‘

>>> b.split(‘..‘)[3]

‘ghn‘

>>> 


本文出自 “不被上秒牵挂不为下秒担忧” 博客,转载请与作者联系!

python split的使用

标签:python split

原文地址:http://ghnhl.blog.51cto.com/1630025/1558586

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!