簡體   English   中英

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

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

我有一個包含大量數據信息的數據文件,我試圖從文件中提取IP時間和長度。 我設法提取時間,但我不確定你如何提取長度和IP。 有沒有辦法搜索,當它看到長度然后它會打印任何長度?

import sys
import string


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

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

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

到目前為止我所擁有的

這是繼續......的數據文件的一部分

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

一種可能的方法是使用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']

另一種選擇是使用Regex

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

所以你的程序改為

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