I have a text file in which there are several variables. Most of them are used in a Bash script of mine, but I'd like to use the same text file for my Python script. For the lines that are not properly formatted for Python, I want my script to just ignore. For those that are properly formatted, I want the script to check and if it's the variable I'm looking for - use it.
import sys import re
for ln in open("thefile.txt"):
m = re.match(r"(?P<varname>[^=]*)\s*=\s*(?P<value>.+)", ln)
if m:
varname = m.group("varname")
value_string = m.group("value")
value = eval(value_string)
print value
# so if the variables name is THISVARIABLE, get that value:
if varname == "THISVARIABLE":
mypythonvariable == value
I'm getting the following error:
NameError: name 'Somevariableinmytextfile' is not defined
The Somevariableinmytextfile is the first variable in that file.
My question:
Do I have to define every variable in the txt file, in order to get rid of this error? If not, what shall I do? I'm very new at Python. This is my first program.
'Somevariableinmytextfile'.eval.