0

I want to output a list like:

operation1 = [
    'command\s+[0-9]+',
]

Where the pattern [0-9]+ is to be dynamically filled.

So I wrote:

reg = {
    'NUMBER' : '^[0-9]+$',
    }

operation1 = [
    'command\s+'+str(reg[NUMBER]),
]
print operation1

But i am getting an error:

Message File Name   Line    Position    
Traceback               
    <module>    <module1>   6       
NameError: name 'NUMBER' is not defined             

Help needed! Thanks in advance.

4 Answers 4

1

it should be reg['NUMBER'], I guess. 'NUMBER' is not a variable

Sign up to request clarification or add additional context in comments.

Comments

0

NUMBER should be a string:

reg['NUMBER']

Comments

0

You're using variable NUMBER, which is not defined. I think what you wanted to use is string 'NUMBER', like this:

>>> operation1 = ['command\s+[0-9]+',]
>>> reg = {'NUMBER' : '^[0-9]+$'}
>>> operation1 = [x + reg['NUMBER'] for x in operation1]
>>> operation1
['command\\s+[0-9]+^[0-9]+$']

Comments

0

You need to put the key in quotes (Perl allows not adding quotes, but not Python):

operation1 = [
    'command\s+'+str(reg['NUMBER']),
]

You also don't need the call to str:

operation1 = [
    'command\s+'+reg['NUMBER'],
]

You could even do this (although not related to the original question):

operation1 = [
    'command\s+{}'.format(reg['NUMBER']),
]

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.