背景和目标
这次我想要处理的是一个txt文件,里面的内容是一台机器定时ping另一台机器的输出结果,想要提取出的内容是时间和rtt值,最后还要把结果输出到csv文件。
1. 明确要提取的内容,编写正则表达式
要提取的文本如下:
第一步是要编写正则表达式,此时可以先不要读取数据文件。先复制一部分数据到str中,方便测试。
编写正则表达式用到了re模块,因为每个人要处理的文本是不一样的,所以需要自己去学习基本的使用方法。re具体使用方法可以参考这篇文章:
https://zhuanlan.zhihu.com/p/139596371
关键就是弄清楚.*?
和{}
的作用,还有re.S
可以匹配到换行符,就可以比较容易地写出正确的表达式。
import re
# 为了方便测试,我把一部分文本先放到str里
str='''
2022-03-11 15:21:48
1
PING 81.71.51.181 (81.71.51.181) 56(84) bytes of data.
64 bytes from 81.71.51.181: icmp_seq=1 ttl=45 time=250 ms
64 bytes from 81.71.51.181: icmp_seq=2 ttl=45 time=250 ms
64 bytes from 81.71.51.181: icmp_seq=3 ttl=45 time=250 ms
64 bytes from 81.71.51.181: icmp_seq=4 ttl=45 time=250 ms
64 bytes from 81.71.51.181: icmp_seq=5 ttl=45 time=250 ms
64 bytes from 81.71.51.181: icmp_seq=6 ttl=45 time=250 ms
64 bytes from 81.71.51.181: icmp_seq=7 ttl=45 time=250 ms
64 bytes from 81.71.51.181: icmp_seq=8 ttl=45 time=250 ms
64 bytes from 81.71.51.181: icmp_seq=9 ttl=45 time=250 ms
64 bytes from 81.71.51.181: icmp_seq=10 ttl=45 time=253 ms
--- 81.71.51.181 ping statistics ---
10 packets transmitted, 10 received, 0% packet loss, time 9000ms
rtt min/avg/max/mdev = 250.203/250.563/253.202/0.961 ms
2022-03-11 15:22:40
2
PING 81.71.51.181 (81.71.51.181) 56(84) bytes of data.
64 bytes from 81.71.51.181: icmp_seq=1 ttl=45 time=250 ms
64 bytes from 81.71.51.181: icmp_seq=2 ttl=45 time=250 ms
64 bytes from 81.71.51.181: icmp_seq=3 ttl=45 time=250 ms
64 bytes from 81.71.51.181: icmp_seq=4 ttl=45 time=250 ms
64 bytes from 81.71.51.181: icmp_seq=5 ttl=45 time=250 ms
64 bytes from 81.71.51.181: icmp_seq=6 ttl=45 time=250 ms
64 bytes from 81.71.51.181: icmp_seq=7 ttl=45 time=250 ms
64 bytes from 81.71.51.181: icmp_seq=8 ttl=45 time=250 ms
64 bytes from 81.71.51.181: icmp_seq=9 ttl=45 time=250 ms
64 bytes from 81.71.51.181: icmp_seq=10 ttl=45 time=250 ms
--- 81.71.51.181 ping statistics ---
10 packets transmitted, 10 received, 0% packet loss, time 9009ms
rtt min/avg/max/mdev = 250.181/250.256/250.434/0.636 ms
2022-03-11 15:23:44
3
PING 81.71.51.181 (81.71.51.181) 56(84) bytes of data.
64 bytes from 81.71.51.181: icmp_seq=1 ttl=45 time=250 ms
64 bytes from 81.71.51.181: icmp_seq=2 ttl=45 time=250 ms
64 bytes from 81.71.51.181: icmp_seq=3 ttl=45 time=250 ms
64 bytes from 81.71.51.181: icmp_seq=4 ttl=45 time=250 ms
64 bytes from 81.71.51.181: icmp_seq=5 ttl=45 time=250 ms
64 bytes from 81.71.51.181: icmp_seq=6 ttl=45 time=250 ms
64 bytes from 81.71.51.181: icmp_seq=7 ttl=45 time=250 ms
64 bytes from