The structure of my code is as follows:
def collect_data(text):
for line in text:
key, match = parse_lines(line) #here I am parsing using a dict and regular expressions
if key == something_1:
var1 = match.group(1)
list1.append(var1)
elif key == something_2:
var2 = match.group(2)
list2.append(var2)
elif key == something_3:
func()
def func():
for element in elements :
if element == element1:
variation = float(var1)
elif element == element2:
variation = float(var2)
#here I append the variable variation to a list depending on the element name e.g. somelist.append(variation)
collect_data(text)
I am trying to parse through lines similar to the solution given in here link. In two first conditionals I append either var1 or var2 to some lists and it works as it should. The problem is with func(). I am getting: NameError: name 'var1' is not defined.
In the text that I am parsing, the first two conditions in collect_data(text) typically occur before the third one but I don't think this is the nature of my problem. I just don't know how to make Python see that var1 and var2 are (will be) defined once the conditions are met. I hope the explanation is clear enough. Perhaps using a class would be a solution? Does anyone have any suggestions?