I am extracting data frome a given text file into a python dictionary for further processing
Input:
---------------------------- SECTION-A ----------------------------
Parameter1 : Text 1
Parameter2 : Text 2
Parameter3 : Text 3
Parameter4 : Text 4
Parameter5 : Text 5
Parameter6 : Text 6
Parameter7 : Text 7
https://www.google.com
---------------------------- SECTION-B ----------------------------
Parameter8 : Text 8
Parameter9 : Text 9
---------------------------- SECTION-C ----------------------------
Item C1
Item C2
Item C3
Item C4
---------------------------- SECTION-D ----------------------------
Lorem ipsum dolor sit amet, consectetur adipiscing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Gravida cum sociis natoque penatibus et magnis dis parturient montes.
Metus dictum at tempor commodo ullamcorper a lacus vestibulum sed.
---------------------------- END ----------------------------
The number of elements in section C and D may vary. I process the data section by section so that i can easily add parameters if there is an extension to the layout.
Code:
import re
from pprint import pprint
def parser(lines):
SECTIONS = [
"SECTION-A",
"SECTION-B",
"SECTION-C",
"SECTION-D",
"END",
]
parameter = {
"Parameter1": "",
"Parameter2": "",
"Parameter3": "",
"Parameter4": "",
"Parameter5": "",
"Parameter6": "",
"Parameter7": "",
"Parameter8": "",
"Parameter9": "",
}
items_c = []
text_d = []
lines = [l.strip() for l in lines]
def find_section(line):
for s in SECTIONS:
if line.find(s) > 0:
return s
return None
def assign_parameter(line):
for p in parameter:
if line.startswith(p):
parameter[p] = line.split(":")[1]
act_section = ""
for line in lines:
line = re.sub(" +", " ", line) # Remove excess spaces
# Keep track of actual section
if (s := find_section(line)) is not None:
act_section = s
# Process lines according to section
if act_section == SECTIONS[0] or act_section == SECTIONS[1]:
assign_parameter(line)
if line.startswith("https://"):
url = line
elif act_section == SECTIONS[2]:
if len(line) > 1 and not line.startswith("--"):
items_c.append(line)
elif act_section == SECTIONS[3]:
if len(line) > 1 and not line.startswith("--"):
text_d.append(line)
# Append data in dictionary
parameter.update({"URL": url})
parameter.update({"Items C": items_c})
parameter.update({"Description D": " ".join(text_d)})
return parameter
if __name__ == "__main__":
with open("test.txt", "r", encoding="utf8") as f:
lines = f.readlines()
pprint(parser(lines))
Output:
{'Description D': 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, '
'sed do eiusmod tempor incididunt ut labore et dolore magna '
'aliqua. Gravida cum sociis natoque penatibus et magnis dis '
'parturient montes. Metus dictum at tempor commodo '
'ullamcorper a lacus vestibulum sed.',
'Items C': ['Item C1', 'Item C2', 'Item C3', 'Item C4'],
'Parameter1': ' Text 1',
'Parameter2': ' Text 2',
'Parameter3': ' Text 3',
'Parameter4': ' Text 4',
'Parameter5': ' Text 5',
'Parameter6': ' Text 6',
'Parameter7': ' Text 7',
'Parameter8': ' Text 8',
'Parameter9': ' Text 9',
'URL': 'https://www.google.com'}
My code is working but what could be improved or optimized?