Reputation: 536
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.
Upvotes: 0
Views: 162
Reputation: 48725
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']),
]
Upvotes: 0
Reputation: 117337
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]+$']
Upvotes: 0
Reputation: 387
it should be reg['NUMBER']
, I guess. 'NUMBER' is not a variable
Upvotes: 1