简体   繁体   English

从打印中提取python中的特定行

[英]Extracting specific lines in python from a printing it

I have a data file with a whole bunch of data information and im trying to extract the IP TIME and length from the file. 我有一个包含大量数据信息的数据文件,我试图从文件中提取IP时间和长度。 I manage to extract time, but i am not sure how you extract the length and IP. 我设法提取时间,但我不确定你如何提取长度和IP。 Is there a way to search through and when it see the length then it would print whatever the length is? 有没有办法搜索,当它看到长度然后它会打印任何长度?

import sys
import string


text_file = file('MyTraceOutput.txt', "r")

for line in text_file:
    columns = line.split(' ')

    if columns:
        print "Time", columns[0]

that what i have so far 到目前为止我所拥有的

This is part of the data file which continues... 这是继续......的数据文件的一部分

reading from file enel573-project1-1-0.pcap, link-type EN10MB (Ethernet)

2.000000 arp who-has 192.168.0.1 (ff:ff:ff:ff:ff:ff) tell 192.168.0.2
2.000023 arp who-has 192.168.0.1 (ff:ff:ff:ff:ff:ff) tell 192.168.0.3
2.000044 arp reply 192.168.0.1 is-at 00:00:00:00:00:01
2.000044 IP (tos 0x0, ttl  64, id 0, offset 0, flags [none], proto: TCP (6), length: 40, bad cksum 0 (->f97c)!) 192.168.0.2.49153 > 192.168.0.1.80: S, cksum 0x0000 (incorrect (-> 0x6e3d), 0:0(0) win 65535
2.000116 arp reply 192.168.0.1 is-at 00:00:00:00:00:01
2.000128 arp who-has 192.168.0.2 (ff:ff:ff:ff:ff:ff) tell 192.168.0.1
2.000128 arp reply 192.168.0.2 is-at 00:00:00:00:00:02
2.000141 IP (tos 0x0, ttl  64, id 0, offset 0, flags [none], proto: TCP (6), length: 40, bad cksum 0 (->f97b)!) 192.168.0.3.49153 > 192.168.0.1.80: S, cksum 0x0000 (incorrect (-> 0x6e3c), 0:0(0) win 65535
2.000152 arp who-has 192.168.0.3 (ff:ff:ff:ff:ff:ff) tell 192.168.0.1
2.000165 arp reply 192.168.0.3 is-at 00:00:00:00:00:03
2.000178 IP (tos 0x0, ttl  64, id 1, offset 0, flags [none], proto: TCP (6), length: 40, bad cksum 0 (->f97a)!) 192.168.0.1.80 > 192.168.0.3.49153: S, cksum 0x0000 (incorrect (-> 0x6e2b), 0:0(0) ack 1 win 65535
2.000189 IP (tos 0x0, ttl  64, id 1, offset 0, flags [none], proto: TCP (6), length: 40, bad cksum 0 (->f97a)!) 192.168.0.3.49153 > 192.168.0.1.80: ., cksum 0x0000 (incorrect (-> 0x6e2c), ack 1 win 65535
2.000202 IP (tos 0x0, ttl  64, id 2, offset 0, flags [none], proto: TCP (6), length: 44, bad cksum 0 (->f975)!) 192.168.0.3.49153 > 192.168.0.1.80: ., cksum 0x0000 (incorrect (-> 0x18d3), 1:5(4) ack 1 win 65535
2.000214 IP (tos 0x0, ttl  64, id 2, offset 0, flags [none], proto: TCP (6), length: 40, bad cksum 0 (->f979)!) 192.168.0.1.80 > 192.168.0.3.49153: ., cksum 0x0000 (incorrect (-> 0x6e28), ack 5 win 65535
2.000253 IP (tos 0x0, ttl  64, id 3, offset 0, flags [none], proto: TCP (6), length: 44, bad cksum 0 (->f974)!) 192.168.0.3.49153 > 192.168.0.1.80: ., cksum 0x0000 (incorrect (-> 0x18cf), 5:9(4

One Possible way to do this is to use str.translate 一种可能的方法是使用str.translate

>>> from string import printable, digits
>>> select_set = ''.join(set(printable) - set(digits + '. '))
>>> st = "2.000000 arp who-has 192.168.0.1 (ff:ff:ff:ff:ff:ff) tell 192.168.0.2"
>>> st.translate(None, select_set).split()
['2.000000', '192.168.0.1', '192.168.0.2']

Another option is to use Regex 另一种选择是使用Regex

>>> import re
>>> re.findall('[\d\.]+',st)
['2.000000', '192.168.0.1', '192.168.0.2']

So your program changes to 所以你的程序改为

import sys
import string


text_file = file('MyTraceOutput.txt', "r")

for line in text_file:
    columns = re.findall('[\d\.]+',line)

    if columns:
        print "Time {}, IP_Range {}-{}".format(*columns)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM