2

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.

5
  • 1
    What's the actual error you're getting? I doubt it literally says 'Somevariableinmytextfile'. Commented Jul 21, 2013 at 21:56
  • I think it is because you use eval. Commented Jul 21, 2013 at 21:58
  • 2
    could you show us few example lines from thefile.txt? Commented Jul 21, 2013 at 21:58
  • The "somevarianble..." is the first variable in my list. At the moment "infothing". Commented Jul 22, 2013 at 12:23
  • A few examples from thefile.txt (one on each row, although I can't make a line break here): infothing=/home/box/ infonumber=125 Commented Jul 22, 2013 at 12:24

3 Answers 3

1

The error is eval complaining that the contents of value_string have no meaning as a whatever-it-is.

The real error is using eval at all. (A good post on the pitfalls can be found here.) You don't even need to eval here - leaving value_string as the string the regex gave you will be just fine.

The problem with the present approach

Sample thefile.txt:

foo=bar
baz=42
quux=import os; os.shutdown()
  • When parsing foo, Python complains that bar isn't defined. (Simple.)
  • When parsing bar, Python gives you an int instead of a str. (No real problem...)
  • When parsing quux, Python shuts down your computer. (Uh oh!)

Why you don't need eval

You want a string value, correct? The regex already gives you a string!

varname = m.group("varname")
value = m.group("value")
print value
if varname == "THISVARIABLE":
    mypythonvariable = value # You meant = instead of ==?
Sign up to request clarification or add additional context in comments.

6 Comments

I did mean = (assign) rather than == (compare). Yes. And thank you very much for a great answer!
@PaoloVacirca The code, with my fixes, works fine for me on Python 2.7.3 (WinRT).
It turns out it's not assigning any value to THISVARIABLE. It's empty. There's something else wrong here, on my end of it. I'm trying to figure out what. :)
@PaoloVacirca Perhaps check if mypythonvariable is empty? (That's what the sample code is assigning to...)
It is empty. I don't understand why though. I must have missed something in the loop.
|
0

You get the error because a line in your thefile.txt file:

TEST = ABC

will be evaluated in python as TEST will be assigned to a value of variable ABC but you have no ABC defined.

You could make a dictionary to store your value pairs... this will work with string values:

variables = {}
accepted = ['THISVARIABLE', 'ANOTHERONE']
...
if varname in accepted:
    variables[varname]=value

Comments

0

eval throws the error, the value_string's value (which should be a variable) needs to be defined before use.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.