This code works fine but is too big, i would like to know if there is any other way to write this code to make it shorter.
import openpyxl as excel
PATH = "/home/Fathima/workspace/training/"
ACCESS_LIST = []
def READ_CONFIG():
FROM_ZONE = ""
TO_ZONE = ""
POLICY_NAME = ""
SOURCE_ADDR = ""
DESTINATION_ADDR = ""
PORT = ""
global PATH
global ACCESS_LIST
count = 0
CONFIG_PATH=PATH+"hofwdcn05dcn_20210216(2).txt"
fh = open(CONFIG_PATH, 'r')
CONFIG_LINES=fh.readlines()
config_lines_cnt = len(CONFIG_LINES)
while count < config_lines_cnt:
line = CONFIG_LINES[count].strip()
if len(line) > 0:
line_to_array = line.split(' ')
if line.startswith('from-zone '):
FROM_ZONE = line_to_array[1]
TO_ZONE = line_to_array[3]
elif line.startswith('policy '):
POLICY_NAME = line_to_array[1]
elif line.startswith('source-address '):
SOURCE_ADDR = line_to_array[1].replace(";", "")
elif line.startswith('destination-address '):
DESTINATION_ADDR = line_to_array[1].replace(";", "")
elif line.startswith('application '):
PORT = line_to_array[1].replace(";", "")
elif line.startswith('then {'):
count = count+1
line = CONFIG_LINES[count].strip()
if line == "permit;":
dummy = { 'FROM_ZONE' : FROM_ZONE,'TO_ZONE' : TO_ZONE,'POLICY_NAME' : POLICY_NAME,'SOURCE_ADDR' : SOURCE_ADDR,'DESTINATION_ADDR' : DESTINATION_ADDR,'PORT' : PORT}
ACCESS_LIST.append(dummy)
FROM_ZONE = ""
TO_ZONE = ""
POLICY_NAME = ""
SOURCE_ADDR = ""
DESTINATION_ADDR = ""
PORT = ""
count +=1
#MAIN PROGRAM STARTS FROM HERE
READ_CONFIG()
print(ACCESS_LIST)
Here i have a huge file and need the output appearing as below format
[{ from-zone: to-zone: policy: source-address: destination-address: application: },{ from-zone: to-zone: policy: source-address: destination-address: application: }]