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‘
>>>
时间: 2024-10-22 00:31:10