我正在使用以下方式读取文件:
def readFile():
file = open('Rules.txt', 'r')
lines = file.readlines()
for line in lines:
rulesList.append(line)
Run Code Online (Sandbox Code Playgroud)
规则列表:
['\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']
Run Code Online (Sandbox Code Playgroud)
我的文件如下所示:
Rule(F1, HTTPS TCP, ['ip', 'ip'], ['www.google.ca', '8.8.8.8'], 443)
Rule(F2, HTTPS TCP, ['ip', 'ip'], ['ip'], 443)
Run Code Online (Sandbox Code Playgroud)
我想将这些值提供给我创建的类
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)
Run Code Online (Sandbox Code Playgroud)
获得如下输出:
[F1, HTTPS TCP, ['ip', 'ip'], ['www.google.ca', '8.8.8.8'], 443]
Run Code Online (Sandbox Code Playgroud)
或者能够将这些值分配给变量,例如:
currentFlowNum = F1, currentProtocol = 'HTTPS TCP' , currentPort = 443, currentFromIP = ['ip', 'ip'], currentToIP = ['www.google.ca', '8.8.8.8']
Run Code Online (Sandbox Code Playgroud)
我试过:
for rule in rulesList:
if rule !='\n':
tmp = rule.split(',')
print(tmp)
Run Code Online (Sandbox Code Playgroud)
临时:
['Rule(F1', ' HTTPS TCP', " ['ip'", " 'ip']", " ['www.google.ca'", " '8.8.8.8']", ' 443)\n']
['Rule(F2', ' HTTPS TCP', " ['ip'", " 'ip']", " ['ip']", ' 443)\n']
Run Code Online (Sandbox Code Playgroud)
有没有办法不分割 [] 之间的逗号,即我希望输出看起来像:
['Rule(F1', ' HTTPS TCP', " ['ip','ip']", " ['www.google.ca', '8.8.8.8']", ' 443)\n']
['Rule(F2', ' HTTPS TCP', " ['ip','ip']", " ['ip']", ' 443)\n']
Run Code Online (Sandbox Code Playgroud)
如果您可以控制文件中数据的存储方式,并且可以用双引号 ( ') 替换单引号 ( ") 以使“列表”结构有效 JSON,则可以使用 RegExp 来实现此目的。
A word of caution: unless you are absolutely sure that the format you'll be reading will largely remain the same and is rather inflexible, you're better off storing this data in a more well-established format (as mentioned in the comments) like JSON, YAML, etc. There are so many edge cases that could happen here that rolling your own parser like this objectively suboptimal.
import re
import json
def readFile():
file = open('Rules.txt', 'r')
myRules = []
for line in file.readlines():
match = re.match(r'Rule\((?P<flow_number>[^,]+),\s(?P<protocol>[^,]+),\s(?P<from_ip>\[[^\]]+\]),\s(?P<to_ip>\[[^\]]+\]),\s(?P<port>[^,)]+)\)', line)
if match:
myRules.append(Rule(match.group('flow_number'), match.group('protocol'), match.group('port'), json.loads(match.group('from_ip')), json.loads(match.group('to_ip'))))
return myRules
print(readFile())
# Returns:
# [
# Rule(F1, HTTPS TCP, ['ip', 'ip'], ['www.google.ca', '8.8.8.8'], 443),
# Rule(F2, HTTPS TCP, ['ip', 'ip'], ['ip'], 443)]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
69 次 |
| 最近记录: |