Python自动化01:字符串匹配

Python自动化:字符串匹配

问题

经常遇到匹配字符串,比如查看文件权限,通信端口矩阵,网页中是否包含文本内容

方法

Python匹配字符串有三个方法:find,startswith,endwith

  1. find:查看log中IP地址
ifconfig_str = '''
eth0      Link encap:Ethernet  HWaddr 00:1C:42:20:ED:B5
          inet addr:10.211.55.9  Bcast:10.211.55.255  Mask:255.255.255.0
          inet6 addr: fdb2:2c26:f4e4:0:972:417e:7886:6d63/64 Scope:Global
          inet6 addr: fdb2:2c26:f4e4:0:21c:42ff:fe20:edb5/64 Scope:Global
          inet6 addr: fe80::21c:42ff:fe20:edb5/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:19303 errors:0 dropped:0 overruns:0 frame:0
          TX packets:10650 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:27642042 (26.3 Mb)  TX bytes:677015 (661.1 Kb)
'''
pos = ifconfig_str.find('10.211.55.9')
if pos == -1:
    print("not find")
else:
    print("find")
  1. startswith
    startswith函数就表示字符串是什么开头的,返回True or False。比如:
	test = 'hello word'
   print( test.startswith('hello') )
   print( test.startswith('world'))
  1. endswith
    endswith函数和startswith函数刚好相反,是表示字符串以什么内容结束
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值