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;








-3















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.










share|improve this question
























  • what is in the variable houses

    – Jeril
    Mar 26 at 5:17











  • I hope now it is clearer

    – Eagle
    Mar 26 at 5:20


















-3















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.










share|improve this question
























  • what is in the variable houses

    – Jeril
    Mar 26 at 5:17











  • I hope now it is clearer

    – Eagle
    Mar 26 at 5:20














-3












-3








-3








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.










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 variable houses

    – 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











  • 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













2 Answers
2






active

oldest

votes


















0














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)





share|improve this answer






























    -1














    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(','))





    share|improve this answer

























    • 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













    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
    );



    );













    draft saved

    draft discarded


















    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









    0














    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)





    share|improve this answer



























      0














      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)





      share|improve this answer

























        0












        0








        0







        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)





        share|improve this answer













        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)






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 27 at 20:34









        EagleEagle

        1,3014 gold badges24 silver badges37 bronze badges




        1,3014 gold badges24 silver badges37 bronze badges























            -1














            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(','))





            share|improve this answer

























            • 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















            -1














            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(','))





            share|improve this answer

























            • 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













            -1












            -1








            -1







            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(','))





            share|improve this answer















            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(','))






            share|improve this answer














            share|improve this answer



            share|improve this answer








            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

















            • 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

















            draft saved

            draft discarded
















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

            은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현