简体   繁体   English

需要从 python 中的列表中提取信息-使用 jupyter notebook

[英]Needing to extract information from a list in python- using jupyter notebook

I am a beginner and I am learning python for my class, The problem I am currently running into is, I am sending a ping to googles server and tracing the time it takes for it to reach back.我是初学者,我正在为我的 class 学习 python,我目前遇到的问题是,我正在向谷歌服务器发送 ping 并跟踪它返回所需的时间。 3 values are given for each step of the ping. ping 的每一步都给出了 3 个值。 I need to convert these 3 values into a average but they are apart of a string with a bunch of other useless random information that I need to sort out.我需要将这 3 个值转换为平均值,但它们是一个字符串的一部分,其中包含我需要整理的一堆其他无用的随机信息。 I'm having trouble with separating these values from the other information.我无法将这些值与其他信息分开。

I have tried using the split() method with new lines to get a list of each individual line and del to delete some of the redundant information, but I'm having trouble with refining it further.我尝试使用带有新行的 split() 方法来获取每个单独行的列表,并使用 del 删除一些冗余信息,但是我在进一步完善它时遇到了麻烦。

import subprocess
from subprocess import check_output, Popen, call, PIPE, STDOUT 

latency  = []

p = Popen('tracert google.com', stdout = PIPE, stderr = STDOUT, shell = True)
for line in p.stdout:
    lntxt = line.decode('utf-8').rstrip()
    print(lntxt)
    words = lntxt.split('\n')
    latency.append(words)
del latency [0:4]
del latency [-4:0]
del latency [-1]
del latency [-1]

L1 = (latency)
L2 = ""
L3 = ""
for x in L1:
    L2 += str(x)
L2.split(" ms  ")

This is what happens when I run the code:这是我运行代码时发生的情况:

Tracing route to google.com [142.250.190.110]
over a maximum of 30 hops:

  1     2 ms     2 ms     1 ms  141.215.192.2
  2     3 ms    14 ms     2 ms  fcn-core-eth-1-4.its.umd.umich.edu 
[141.215.2.202]
  3     3 ms     2 ms     2 ms  fcn-edge-merit-primary.its.umd.umich.edu 
[141.215.2.103]
  4     4 ms    10 ms     3 ms  drbr-fairlane-d1.its.umd.umich.edu 
[141.215.2.56]
  5     3 ms     2 ms     2 ms  ae1x78.dtrt-wsudc-c1.mich.net 
[207.72.237.28]
  6    11 ms    10 ms    16 ms  ae6x14.chcg-lvl3-600w.mich.net 
[198.108.22.13]
  7    11 ms    11 ms    11 ms  192.35.170.66
  8    13 ms    12 ms    12 ms  209.85.250.189
  9    11 ms    11 ms    11 ms  142.251.60.205
 10    11 ms    11 ms    11 ms  ord37s35-in-f14.1e100.net 
[142.250.190.110]

Trace complete.
["['  1     2",
 '   2',
 '   1',
 "141.215.192.2']['  2     3",
 '  14',
 '   2',
 "fcn-core-eth-1-4.its.umd.umich.edu [141.215.2.202]']['  3     3",
 '   2',
 '   2',
 "fcn-edge-merit-primary.its.umd.umich.edu [141.215.2.103]']['  4     
4",
 '  10',
 '   3',
 "drbr-fairlane-d1.its.umd.umich.edu [141.215.2.56]']['  5     3",
 '   2',
 '   2',
 "ae1x78.dtrt-wsudc-c1.mich.net [207.72.237.28]']['  6    11",
 '  10',
 '  16',
 "ae6x14.chcg-lvl3-600w.mich.net [198.108.22.13]']['  7    11",
 '  11',
 '  11',
 "192.35.170.66']['  8    13",
 '  12',
 '  12',
 "209.85.250.189']['  9    11",
 '  11',
  '  11',
 "142.251.60.205'][' 10    11",
 '  11',
 '  11',
 "ord37s35-in-f14.1e100.net [142.250.190.110]']"]

you need to process each line, searching between two characters or using regular expressions in order to extract the desired value.您需要处理每一行,在两个字符之间搜索或使用正则表达式以提取所需的值。

I give you a functional example:我给你一个功能性的例子:

from subprocess import check_output, Popen, call, PIPE, STDOUT 

latency  = []

def call_traceroute(url: str):
    p = Popen(f'traceroute {url}', stdout = PIPE, stderr = STDOUT, shell = True)
    lines = []
    for line in p.stdout:
        lines.append(line.decode('utf-8').rstrip())
    lines = lines[1:]
    print(lines)
    return lines

def extract_string_between_two_characters(original_string: str, start_character: str, end_character: str):
    return original_string[original_string.find(start_character)+len(start_character):original_string.find(end_character)]

def extract_latency(line: str):
    latency = extract_string_between_two_characters(original_string=line, start_character =") ", end_character="ms")
    return latency

all_steps = call_traceroute('google.com')
print("\n\n")
for index, step in enumerate(all_steps):
    print(f"Original Traceroute line {step}")
    latency = extract_latency(step)
    print(f"Latency in step: {index} is {latency} ms\n\n")

An output example is: output 示例是:

python3 latency.py
[' 1  192.168.1.1 (192.168.1.1)  10.005 ms  3.198 ms  3.406 ms', ' 2  10.0.0.1 (10.0.0.1)  4.231 ms  4.078 ms  4.376 ms', ' 3  172.16.8.193 (172.16.8.193)  4.896 ms  4.977 ms  4.772 ms', ' 4  10.220.100.48 (10.220.100.48)  9.616 ms  9.731 ms  11.255 ms', ' 5  74.125.119.226 (74.125.119.226)  11.714 ms  14.836 ms  10.961 ms', ' 6  74.125.242.161 (74.125.242.161)  11.411 ms  11.846 ms', '    74.125.242.177 (74.125.242.177)  11.894 ms', ' 7  142.250.213.125 (142.250.213.125)  11.794 ms  11.892 ms  11.388 ms', ' 8  mad07s23-in-f14.1e100.net (142.250.184.174)  13.050 ms  10.042 ms  11.020 ms']



Original Traceroute line  1  192.168.1.1 (192.168.1.1)  10.005 ms  3.198 ms  3.406 ms
Latency in step: 0 is  10.005  ms


Original Traceroute line  2  10.0.0.1 (10.0.0.1)  4.231 ms  4.078 ms  4.376 ms
Latency in step: 1 is  4.231  ms


Original Traceroute line  3  172.16.8.193 (172.16.8.193)  4.896 ms  4.977 ms  4.772 ms
Latency in step: 2 is  4.896  ms


Original Traceroute line  4  10.220.100.48 (10.220.100.48)  9.616 ms  9.731 ms  11.255 ms
Latency in step: 3 is  9.616  ms


Original Traceroute line  5  74.125.119.226 (74.125.119.226)  11.714 ms  14.836 ms  10.961 ms
Latency in step: 4 is  11.714  ms


Original Traceroute line  6  74.125.242.161 (74.125.242.161)  11.411 ms  11.846 ms
Latency in step: 5 is  11.411  ms


Original Traceroute line     74.125.242.177 (74.125.242.177)  11.894 ms
Latency in step: 6 is  11.894  ms


Original Traceroute line  7  142.250.213.125 (142.250.213.125)  11.794 ms  11.892 ms  11.388 ms
Latency in step: 7 is  11.794  ms


Original Traceroute line  8  mad07s23-in-f14.1e100.net (142.250.184.174)  13.050 ms  10.042 ms  11.020 ms
Latency in step: 8 is  13.050  ms

在此处输入图像描述

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

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