python3 split rsplit splitline函数用法

split: 从左往右分割

rsplit:从右往左分割

splitline: "\n"分割

一 split  rsplit 简单示例:

str_temp = "aaa.bbb.cc.dd"
ret1,ret2 = str_temp.rsplit(".",maxsplit=1)
ret3,ret4 = str_temp.split(".",maxsplit=1)
ret5,ret6,ret7 = str_temp.split(".",maxsplit=2)
print(ret1)
print(ret2)
print(ret3)
print(ret4)
print(ret5)
print(ret6)
print(ret7)

结果:

C:\Python37\python.exe C:/Users/mayn/PycharmProjects/ray/d8/test.py
aaa.bbb.cc
dd
aaa
bbb.cc.dd
aaa
bbb
cc.dd

二. splitline示例(获取本机IP地址):

#获取本机IP地址
import subprocess
ret = subprocess.getoutput("ipconfig") #systeminfo
#print(ret)
new_ret = ret.splitlines()
print(new_ret)

结果:

['', 'Windows IP 配置', '', '', 
'以太网适配器 以太网 2:',
 '', 
'   连接特定的 DNS 后缀 . . . . . . . : DHCP HOST', 
'   本地链接 IPv6 地址. . . . . . . . : fe80::35a8:fdf4:s345:8sdf6%7', 
'   IPv4 地址 . . . . . . . . . . . . : 192.168.0.105',

 

你可能感兴趣的:(Python)