I have a string (str_json) that I’m getting from elsewhere : This is a string str_json which is supposed to be JSON but it actually contains some python code and a number value and single quotes. json.loads will fail. What I need is the JSON string of str_json. json_data = json.loads(str_json) What I’ve attempted so far : Answer You can
Tag: eval
Beginner to python or programming as a whole, getting this error and unable to solve it
Here is the code: here is the error: Answer Python’s eval() allows you to evaluate arbitrary Python expressions from a string-based or compiled-code-based input. However, as the comments correctly say, this is best avoided. All you need to do from here is convert strings to integers. This would work better:
How to make menu using dict evaluation for small python guessing game
So let’s say if user inputs 1 in menu screen, I want to call start_game() function, I know there is other way to do this but just interested in this particular way because I think its neat. Answer Don’t put strings in the dictionary, put references to the function. Then you can do: to execute the user’s choice.
How do I numerically integrate a string with variables in python?
Disclaimer: total noob here, please keep it as simple as possible My plan was to use scipy.integrate.quad() and eval() (code inection won’t be a problem in my case). This works fine when the only variable in the equation is the integration variable: Adding addtional variables works as long as they are defined: However if i try to give eval() those
Using eval within a dict
I want to evaluate the value of a dict key, using the dict itself. For example: When I do this it includes a bunch of unnecessary stuff in the dict. In the above example it prints: Instead, in the above example I just want: How to resolve this issue? Thank you! Answer Pass a copy of dict to eval(): Prints:
“NameError: name ‘numpy’ is not defined” when calling eval()
The line for e2 (but not e1) generates an error: Why? Answer I solved it by replacing: with:
math module not working in a string for eval
Suppose I have this code: but I only get this error: NameError: name ‘math’ is not defined How can I fix this? Answer The second parameter to eval, if present, should reference the global namespace. Here, you explicitely assigned an empty dict to it, so you don’t have any access to the name math of the global namespace. You could
Calling round(), ceiling(), floor(), min(), max() in pandas eval
As title says, Is there a way to support round, ceiling, min, max, floor functions in pandas eval. DataFrame: abs() and sqrt() function works with pandas eval. i.e. can anyone suggest how to access rest of the functions in eval? I also tried ‘local_dict’ in eval to see if I can define custom functions and call them but it didn’t
AttributeError: ‘PandasExprVisitor’ object has no attribute ‘visit_Ellipsis’, using pandas eval
I have a series of the form: Note that its elements are strings: I’m trying to use pd.eval to parse this string into a column of lists. This works for this sample data. However, on much larger data (order of 10K), this fails miserably! What am I missing here? Is there something wrong with the function or my data? Answer
Using python’s eval() vs. ast.literal_eval()
I have a situation with some code where eval() came up as a possible solution. Now I have never had to use eval() before but, I have come across plenty of information about the potential danger it can cause. That said, I’m very wary about using it. My situation is that I have input being given by a user: Where