I am reading a file using:
def readFile():
file = open('Rules.txt', 'r')
lines = file.readlines()
for line in lines:
rulesList.append(line)
rulesList:
['\n', "Rule(F1, HTTPS TCP, ['ip', 'ip'], ['www.google.ca', '8.8.8.8'], 443)\n", '\n', "Rule(F2, HTTPS TCP, ['ip', 'ip'], ['75.2.18.233'], 443)\n", '\n']
My file looks like:
Rule(F1, HTTPS TCP, ['ip', 'ip'], ['www.google.ca', '8.8.8.8'], 443)
Rule(F2, HTTPS TCP, ['ip', 'ip'], ['ip'], 443)
I would like to feed the values to a class I created
class Rule:
def __init__(self, flowNumber, protocol, port, fromIP=[], toIP=[]):
self.flowNumber = flowNumber
self.protocol = protocol
self.port = port
self.fromIP = fromIP
self.toIP = toIP
def __repr__(self):
return f'\nRule({self.flowNumber}, {self.protocol}, {self.fromIP}, {self.toIP}, {self.port})'
newRule = Rule(currentFlowNum, currentProtocol, currentPort, currentFromIP, currentToIP)
to get an output such as:
[F1, HTTPS TCP, ['ip', 'ip'], ['www.google.ca', '8.8.8.8'], 443]
or be able to assign these values to a variable like:
currentFlowNum = F1, currentProtocol = 'HTTPS TCP' , currentPort = 443, currentFromIP = ['ip', 'ip'], currentToIP = ['www.google.ca', '8.8.8.8']
I tried:
for rule in rulesList:
if rule !='\n':
tmp = rule.split(',')
print(tmp)
tmp:
['Rule(F1', ' HTTPS TCP', " ['ip'", " 'ip']", " ['www.google.ca'", " '8.8.8.8']", ' 443)\n']
['Rule(F2', ' HTTPS TCP', " ['ip'", " 'ip']", " ['ip']", ' 443)\n']
Is there a way to not split the commas between [] i.e. I would like the output to look like:
['Rule(F1', ' HTTPS TCP', " ['ip','ip']", " ['www.google.ca', '8.8.8.8']", ' 443)\n']
['Rule(F2', ' HTTPS TCP', " ['ip','ip']", " ['ip']", ' 443)\n']