evaluate a python string expression using dictionary valuesHow to merge two dictionaries in a single expression?Calling an external command in PythonWhat are metaclasses in Python?How can I safely create a nested directory?Does Python have a ternary conditional operator?How do I sort a dictionary by value?Add new keys to a dictionary?Check if a given key already exists in a dictionaryIterating over dictionaries using 'for' loopsDoes Python have a string 'contains' substring method?
Did 007 exist before James Bond?
What is the superlative of ipse?
Why did Spider-Man take a detour to Dorset?
Is there any way for an Adventurers League, 5th level Wizard, to gain heavy armor proficiency?
What do these three diagonal lines that cross through three measures and both staves mean, and what are they called?
Draw a line nicely around notes
What's the phrasal verb for carbonated drinks exploding out of the can after being shaken?
Why aren't globular clusters disk shaped
How to honestly answer questions from a girlfriend like "How did you find this place" without giving the impression I'm always talking about my exes?
What is the German word or phrase for "village returning to forest"?
Can a Resident Assistant Be Told to Ignore a Lawful Order?
Was all the fuel expended in each stage of a Saturn V launch?
Is this more than a packing puzzle?
What is this old "lemon-squeezer" shaped pan
Can't update Ubuntu 18.04.2
Video editor for YouTube
I gave my characters names that are exactly like another book. Is it a problem?
Is it rude to refer to janitors as 'floor people'?
Why do candidates not quit if they no longer have a realistic chance to win in the 2020 US presidents election
Can a pizza stone be fixed after soap has been used to clean it?
MQTT subscription topic match
Could I use a greatsword and a longsword in one turn with Two-Weapon Fighting and the Dual Wielder feat?
Conducting exams in which a computer (but no internet) is available
What exactly is a Hadouken?
evaluate a python string expression using dictionary values
How to merge two dictionaries in a single expression?Calling an external command in PythonWhat are metaclasses in Python?How can I safely create a nested directory?Does Python have a ternary conditional operator?How do I sort a dictionary by value?Add new keys to a dictionary?Check if a given key already exists in a dictionaryIterating over dictionaries using 'for' loopsDoes Python have a string 'contains' substring method?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I am parsing a text file which contain python "string" inside it. For e.g.:
'my_home1' in houses.split(',') and '2018' in iphone.split(',') and 14 < mask
for the example above, I wrote a possible dictionary:
dict = 'house' : 'my_home1,my_home2', 'iphone' : '2015,2018', 'mask' : '15'
but dict
is not know in advance and will be adapted during the running time of the python script.
I would like to replace all the appearance of keys inside the string above in a dynamic way and then to evaluate the expression.
python python-3.x
add a comment |
I am parsing a text file which contain python "string" inside it. For e.g.:
'my_home1' in houses.split(',') and '2018' in iphone.split(',') and 14 < mask
for the example above, I wrote a possible dictionary:
dict = 'house' : 'my_home1,my_home2', 'iphone' : '2015,2018', 'mask' : '15'
but dict
is not know in advance and will be adapted during the running time of the python script.
I would like to replace all the appearance of keys inside the string above in a dynamic way and then to evaluate the expression.
python python-3.x
what is in the variablehouses
– Jeril
Mar 26 at 5:17
I hope now it is clearer
– Eagle
Mar 26 at 5:20
add a comment |
I am parsing a text file which contain python "string" inside it. For e.g.:
'my_home1' in houses.split(',') and '2018' in iphone.split(',') and 14 < mask
for the example above, I wrote a possible dictionary:
dict = 'house' : 'my_home1,my_home2', 'iphone' : '2015,2018', 'mask' : '15'
but dict
is not know in advance and will be adapted during the running time of the python script.
I would like to replace all the appearance of keys inside the string above in a dynamic way and then to evaluate the expression.
python python-3.x
I am parsing a text file which contain python "string" inside it. For e.g.:
'my_home1' in houses.split(',') and '2018' in iphone.split(',') and 14 < mask
for the example above, I wrote a possible dictionary:
dict = 'house' : 'my_home1,my_home2', 'iphone' : '2015,2018', 'mask' : '15'
but dict
is not know in advance and will be adapted during the running time of the python script.
I would like to replace all the appearance of keys inside the string above in a dynamic way and then to evaluate the expression.
python python-3.x
python python-3.x
edited Mar 26 at 10:28
Eagle
asked Mar 26 at 5:10
EagleEagle
1,3014 gold badges24 silver badges37 bronze badges
1,3014 gold badges24 silver badges37 bronze badges
what is in the variablehouses
– Jeril
Mar 26 at 5:17
I hope now it is clearer
– Eagle
Mar 26 at 5:20
add a comment |
what is in the variablehouses
– Jeril
Mar 26 at 5:17
I hope now it is clearer
– Eagle
Mar 26 at 5:20
what is in the variable
houses
– Jeril
Mar 26 at 5:17
what is in the variable
houses
– Jeril
Mar 26 at 5:17
I hope now it is clearer
– Eagle
Mar 26 at 5:20
I hope now it is clearer
– Eagle
Mar 26 at 5:20
add a comment |
2 Answers
2
active
oldest
votes
At the end I found a solution to my problem:
text = "'my_home1' in houses.split(',') and '2018' in iphone.split(',') and 14 < mask"
dict = 'house' : 'my_home1,my_home2', 'iphone' : '2015,2018', 'mask' : '15'
expression = 'false'
for key in dict.keys():
if isinstance(dict.get(key), str):
text = re.sub(key, ''''.format(dict.get(key)), text)
else:
text = re.sub(key, dict.get(key), text)
eval(text)
add a comment |
Is the following what you were expecting:
dict_ = 'house': 'my_home1,my_home2', 'iphone': '2015,2018'
print('my_home1' in dict_['house'].split(',') and '2018' in dict_[
'iphone'].split(',') and '2019' not in dict_['iphone'].split(','))
thanks for your answer, unfortunately this is not what i am searching. I updated my question.
– Eagle
Mar 26 at 7:22
i have updated my solution, check now
– Jeril
Mar 26 at 7:26
my difficulty with your solution is that I do not know in advance the keys inside dict.
– Eagle
Mar 26 at 7:33
i am not able to get you, can you come again
– Jeril
Mar 26 at 9:28
I have adapted my example above. The difficulty is that you you do not know which textsstrings in the expressions are in the dictionary and need to be evaluate with 'dict_'.
– Eagle
Mar 26 at 10:35
|
show 1 more comment
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55350225%2fevaluate-a-python-string-expression-using-dictionary-values%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
At the end I found a solution to my problem:
text = "'my_home1' in houses.split(',') and '2018' in iphone.split(',') and 14 < mask"
dict = 'house' : 'my_home1,my_home2', 'iphone' : '2015,2018', 'mask' : '15'
expression = 'false'
for key in dict.keys():
if isinstance(dict.get(key), str):
text = re.sub(key, ''''.format(dict.get(key)), text)
else:
text = re.sub(key, dict.get(key), text)
eval(text)
add a comment |
At the end I found a solution to my problem:
text = "'my_home1' in houses.split(',') and '2018' in iphone.split(',') and 14 < mask"
dict = 'house' : 'my_home1,my_home2', 'iphone' : '2015,2018', 'mask' : '15'
expression = 'false'
for key in dict.keys():
if isinstance(dict.get(key), str):
text = re.sub(key, ''''.format(dict.get(key)), text)
else:
text = re.sub(key, dict.get(key), text)
eval(text)
add a comment |
At the end I found a solution to my problem:
text = "'my_home1' in houses.split(',') and '2018' in iphone.split(',') and 14 < mask"
dict = 'house' : 'my_home1,my_home2', 'iphone' : '2015,2018', 'mask' : '15'
expression = 'false'
for key in dict.keys():
if isinstance(dict.get(key), str):
text = re.sub(key, ''''.format(dict.get(key)), text)
else:
text = re.sub(key, dict.get(key), text)
eval(text)
At the end I found a solution to my problem:
text = "'my_home1' in houses.split(',') and '2018' in iphone.split(',') and 14 < mask"
dict = 'house' : 'my_home1,my_home2', 'iphone' : '2015,2018', 'mask' : '15'
expression = 'false'
for key in dict.keys():
if isinstance(dict.get(key), str):
text = re.sub(key, ''''.format(dict.get(key)), text)
else:
text = re.sub(key, dict.get(key), text)
eval(text)
answered Mar 27 at 20:34
EagleEagle
1,3014 gold badges24 silver badges37 bronze badges
1,3014 gold badges24 silver badges37 bronze badges
add a comment |
add a comment |
Is the following what you were expecting:
dict_ = 'house': 'my_home1,my_home2', 'iphone': '2015,2018'
print('my_home1' in dict_['house'].split(',') and '2018' in dict_[
'iphone'].split(',') and '2019' not in dict_['iphone'].split(','))
thanks for your answer, unfortunately this is not what i am searching. I updated my question.
– Eagle
Mar 26 at 7:22
i have updated my solution, check now
– Jeril
Mar 26 at 7:26
my difficulty with your solution is that I do not know in advance the keys inside dict.
– Eagle
Mar 26 at 7:33
i am not able to get you, can you come again
– Jeril
Mar 26 at 9:28
I have adapted my example above. The difficulty is that you you do not know which textsstrings in the expressions are in the dictionary and need to be evaluate with 'dict_'.
– Eagle
Mar 26 at 10:35
|
show 1 more comment
Is the following what you were expecting:
dict_ = 'house': 'my_home1,my_home2', 'iphone': '2015,2018'
print('my_home1' in dict_['house'].split(',') and '2018' in dict_[
'iphone'].split(',') and '2019' not in dict_['iphone'].split(','))
thanks for your answer, unfortunately this is not what i am searching. I updated my question.
– Eagle
Mar 26 at 7:22
i have updated my solution, check now
– Jeril
Mar 26 at 7:26
my difficulty with your solution is that I do not know in advance the keys inside dict.
– Eagle
Mar 26 at 7:33
i am not able to get you, can you come again
– Jeril
Mar 26 at 9:28
I have adapted my example above. The difficulty is that you you do not know which textsstrings in the expressions are in the dictionary and need to be evaluate with 'dict_'.
– Eagle
Mar 26 at 10:35
|
show 1 more comment
Is the following what you were expecting:
dict_ = 'house': 'my_home1,my_home2', 'iphone': '2015,2018'
print('my_home1' in dict_['house'].split(',') and '2018' in dict_[
'iphone'].split(',') and '2019' not in dict_['iphone'].split(','))
Is the following what you were expecting:
dict_ = 'house': 'my_home1,my_home2', 'iphone': '2015,2018'
print('my_home1' in dict_['house'].split(',') and '2018' in dict_[
'iphone'].split(',') and '2019' not in dict_['iphone'].split(','))
edited Mar 26 at 7:26
answered Mar 26 at 5:45
JerilJeril
3,5042 gold badges22 silver badges42 bronze badges
3,5042 gold badges22 silver badges42 bronze badges
thanks for your answer, unfortunately this is not what i am searching. I updated my question.
– Eagle
Mar 26 at 7:22
i have updated my solution, check now
– Jeril
Mar 26 at 7:26
my difficulty with your solution is that I do not know in advance the keys inside dict.
– Eagle
Mar 26 at 7:33
i am not able to get you, can you come again
– Jeril
Mar 26 at 9:28
I have adapted my example above. The difficulty is that you you do not know which textsstrings in the expressions are in the dictionary and need to be evaluate with 'dict_'.
– Eagle
Mar 26 at 10:35
|
show 1 more comment
thanks for your answer, unfortunately this is not what i am searching. I updated my question.
– Eagle
Mar 26 at 7:22
i have updated my solution, check now
– Jeril
Mar 26 at 7:26
my difficulty with your solution is that I do not know in advance the keys inside dict.
– Eagle
Mar 26 at 7:33
i am not able to get you, can you come again
– Jeril
Mar 26 at 9:28
I have adapted my example above. The difficulty is that you you do not know which textsstrings in the expressions are in the dictionary and need to be evaluate with 'dict_'.
– Eagle
Mar 26 at 10:35
thanks for your answer, unfortunately this is not what i am searching. I updated my question.
– Eagle
Mar 26 at 7:22
thanks for your answer, unfortunately this is not what i am searching. I updated my question.
– Eagle
Mar 26 at 7:22
i have updated my solution, check now
– Jeril
Mar 26 at 7:26
i have updated my solution, check now
– Jeril
Mar 26 at 7:26
my difficulty with your solution is that I do not know in advance the keys inside dict.
– Eagle
Mar 26 at 7:33
my difficulty with your solution is that I do not know in advance the keys inside dict.
– Eagle
Mar 26 at 7:33
i am not able to get you, can you come again
– Jeril
Mar 26 at 9:28
i am not able to get you, can you come again
– Jeril
Mar 26 at 9:28
I have adapted my example above. The difficulty is that you you do not know which textsstrings in the expressions are in the dictionary and need to be evaluate with 'dict_'.
– Eagle
Mar 26 at 10:35
I have adapted my example above. The difficulty is that you you do not know which textsstrings in the expressions are in the dictionary and need to be evaluate with 'dict_'.
– Eagle
Mar 26 at 10:35
|
show 1 more comment
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55350225%2fevaluate-a-python-string-expression-using-dictionary-values%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
what is in the variable
houses
– Jeril
Mar 26 at 5:17
I hope now it is clearer
– Eagle
Mar 26 at 5:20