lod*_*kkx 4 python regex windows
我使用以下代码ping网站以检查连接.如何解析结果以获得"Lost ="以查看丢失了多少?
def pingTest():
host = "www.wired.com"
ping = subprocess.Popen(
["ping","-n","4",host],
stdout = subprocess.PIPE,
stderr = subprocess.PIPE
)
out,error = ping.communicate()
print out
Run Code Online (Sandbox Code Playgroud)
这是我从中获得的回报
Pinging wired.com [173.223.232.42] with 32 bytes of data:
Reply from 173.223.232.42: bytes=32 time=54ms TTL=51
Reply from 173.223.232.42: bytes=32 time=54ms TTL=51
Reply from 173.223.232.42: bytes=32 time=54ms TTL=51
Reply from 173.223.232.42: bytes=32 time=54ms TTL=51
Ping statistics for 173.223.232.42:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 54ms, Maximum = 54ms, Average = 54ms
Run Code Online (Sandbox Code Playgroud)
第1步:构造一个匹配的正则表达式Lost = 0 (0% loss)
,使用\d
标签替换数值,这些数值会有所不同.使用捕获组来保留这些值.
第2步:使用re.search扫描out
字符串.
第3步:从re的捕获组中提取值.
归档时间: |
|
查看次数: |
1227 次 |
最近记录: |