简体   繁体   中英

Extract specific values out of string in Python

I am trying to take out the total number of IP addresses and the amount of hosts up from an .nmap file. In the following example I want to take out the value "512" and the value of "17".

# Nmap done at Tue Nov 27 10:09:18 2012 -- 512 IP addresses (17 hosts up) scanned in 143.58 seconds

I then want to store those values in two separate array's (total, online) so I can calculate a running total. I am having trouble coming up with a way to take out those specific values from that line. It will not always be 512 or 17; it could be smaller/larger.

I need to accomplish this in python. I already have code that parses through the file line by line. I just need a way to take out that data.

Any help will be much appreciated.

Thank you!

something like this:

In [53]: strs="# Nmap done at Tue Nov 27 10:09:18 2012 -- 512 IP addresses (17 hosts up) scanned in 143.58 seconds"

In [55]: re.findall("(\d+)\s+IP address",strs)
Out[55]: ['512']

In [56]: re.findall("(\d+)\s+hosts up",strs)
Out[56]: ['17']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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