I have a list of strings that I pulled from a text file. I need to read each line and "select" two specific parts. Here is an example line from the text file (firewall report):
2011-04-13 08:52:55 Local4.Info 192.168.1.1 :Apr 13 08:52:55 PDT: %ASA-session-6-302014: Teardown TCP connection 41997800 for Workstations:192.168.2.85/1440 to Servers:192.168.1.6/43032 duration 0:00:00 bytes 2093 TCP FINs
I need to save the IP address that comes after "Workstations:" and know that they are the "workstation IPs" and I need to save the server IPs as such as well.
I imagine the best technique would be to create two lists, one for workstation IPs and one for server IPs, and read each line and write the IPs to their respective lists.
But in order to do that I need to select them, which I might do like this:
workstationIPs = []
serverIPs = []
for line in report:
workstationIPs.append(line[a:b])
serverIPs.append(line[c:d])
With 'a' being the start of the workstation IP and 'b' being the end (and 'c' and 'd' relating to server IPs).
However, not all the lines are the same length, so that method of selection won't work. Does anyone have any ideas on how to extract those two strings from the line?
PS: this is my first question, so please let me know of errors and I can resubmit it. Thanks!)
import re. Or you can split your line into parts with thesplitmethod of a string object and then extract the elements you want. But the best way is probably a regex