Linked Questions
169 questions linked to/from Using python's eval() vs. ast.literal_eval()
1 vote
2 answers
2k views
Execute python code inside a string and store it to a variable [duplicate]
I'm trying to make a python program that when you input a code, it executes it. It has 2 problems and I need to fix it. I want it when you executed the code, it gets stored in a variable I want't the ...
0 votes
0 answers
494 views
is there an opposite function for str() in python? [duplicate]
str() turns data into strings but is it possible to undo this with a built in function? exe: str1 = '{"a": 1, "b": 2}' is there a function that can turn this into a dictionary? ...
2 votes
0 answers
78 views
How to use eval() and compile() in python 3.7? [duplicate]
I want to evaluate an expression in Python 3.7 that is read as string from a config file. The application is a Datalogger and the user shall be able to define his own conversion of the raw ADC value ...
350 votes
12 answers
545k views
What does Python's eval() do?
The book that I am reading on Python repeatedly shows code like eval(input('blah')). How exactly does this modify the result from calling input? See also: Why is using 'eval' a bad practice? ...
188 votes
9 answers
166k views
Process escape sequences in a string in Python
Sometimes when I get input from a file or the user, I get a string with escape sequences in it. I would like to process the escape sequences in the same way that Python processes escape sequences in ...
160 votes
9 answers
157k views
Pandas DataFrame stored list as string: How to convert back to list
I have an n-by-m Pandas DataFrame df defined as follows. (I know this is not the best way to do it. It makes sense for what I'm trying to do in my actual code, but that would be TMI for this post so ...
59 votes
5 answers
187k views
Malformed String ValueError ast.literal_eval() with String representation of Tuple
I'm trying to read in a string representation of a Tuple from a file, and add the tuple to a list. Here's the relevant code. raw_data = userfile.read().split('\n') for a in raw_data : print a ...
23 votes
6 answers
50k views
specifying a list as a command line argument in python
I am using getopt to process a command line optional argument, which should accept a list. Something like this: foo.py --my_list=[1, 2, 3, 4,5] But this trims everything after "[1," My questions ...
27 votes
3 answers
139k views
ValueError: malformed node or string
Why do I get this error message: ValueError: malformed node or string when I pass data in the below format into the "parse_webhook" function? Thanks! webhook_data = {"side": "...
5 votes
4 answers
70k views
Python - convert a string to an array
How would I convert the following string to an array with Python (this string could have an indefinite number of items)? '["Foo","Bar","Baz","Woo"]' This is ...
23 votes
1 answer
16k views
Reverse repr function in Python [duplicate]
if I have a string with characters ( 0x61 0x62 0xD ), the repr function of this string will return 'ab\r'. Is there way to do reverse operation: if I have string 'ab\r' (with characters 0x61 0x62 ...
7 votes
5 answers
71k views
Create a tuple from an input in Python
Here is my example: >>> a=input ('some text : ') # value entered is 1,1 >>> print (a) 1,1 I want as a result a tuple (1, 1) How can I do this?
11 votes
5 answers
48k views
Python how convert single quotes to double quotes to format as json string [duplicate]
I have a file where on each line I have text like this (representing cast of a film): [{'cast_id': 23, 'character': "Roger 'Verbal' Kint", 'credit_id': '52fe4260c3a36847f8019af7', 'gender': 2, 'id': ...
7 votes
7 answers
11k views
Multiply every element of a list by a number
I would like to multiply all elements of a list by a number. I know the other ways to do it but I want to know Why isn't this working? I am getting the very same list as an output. lst = eval(input('...
21 votes
1 answer
29k views
Convert a string tuple to a tuple [duplicate]
I have a user entered string which is already in tuple format and would like to convert/cast it to a actual tuple in python. How can I do this? E.g: strTup = '(5, 6)' Would like to convert the above ...