Linked Questions
12 questions linked to/from ValueError: malformed string when using ast.literal_eval
2775 votes
33 answers
4.9m views
How do I parse a string to a float or int?
How can I convert an str to a float? "545.2222" -> 545.2222 Or an str to a int? "31" -> 31 For the reverse, see Convert integer to string in Python and Converting a float to ...
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 ...
29 votes
2 answers
3k views
Evaluate math equations from unsafe user input in Python
I have a website where the user enters math equations (expressions) and then those equations are evaluated against data (constants) provided by the website. The math operations needed include symbols, ...
9 votes
2 answers
4k views
Using ast and whitelists to make python's eval() safe?
OK. I know the experts have spoken and you should not ever use python's eval() on untrusted data, ever. I'm not smarter than the rest of the world, and shouldn't even try this. But! I'm going to, ...
4 votes
3 answers
2k views
Pandas DataFrame: Cannot convert string into a float
I have a column Column1 in a pandas dataframe which is of type str, values which are in the following form: import pandas as pd df = pd.read_table("filename.dat") type(df["Column1"].ix[0]) #...
3 votes
2 answers
1k views
Python: Mathematical Operations using Operators from a list
Say I have a list with mathematical operators in it, like this operators = ['+','-','*','/'] And I am taking a random operator from here, like this op = random.choice(operators) If I take two ...
0 votes
2 answers
1k views
Graph Equation From Input
I'm trying to display a graph with a formula from the user using matplotlib in Python 3.6, but I'm having some problems. I tried import numpy as np import matplotlib.pyplot as plt def graph(x_range):...
-1 votes
3 answers
296 views
Conversion and evaluation of strings in python
How can I get a Python list from a text file with the following contents? 'hallo' 'hallo\n' '\x00' * 1 100 '400 + 2' 400 + 2 For example: ll = ["hallo", "hallo\n", "\x00", 100, 402, 402] with the ...
0 votes
0 answers
571 views
Pandas ast.literal_eval crashes with non-basic datatypes while reading lists from csv
I have a Pandas dataframe that was saved as a csv by using command: file.to_csv(filepath + name + ".csv", encoding='utf-8', sep=";", quoting=csv.QUOTE_NONNUMERIC) I know that while saving, all of the ...
0 votes
3 answers
121 views
How would you parse a list in python to combine integer while leaving the operator of an equation alone
How would you parse a list in python to combine integer while leaving the operator of an equation alone? list =['(' ,'1', '+', '2', ')', '+', '(', '2', '0'. '0', '/', '2', '5', ')'] I would like to ...
1 vote
1 answer
125 views
could not convert string to float 12 + 13 [duplicate]
I am trying to write a very simple code which I will feed it two numbers (with the input function) and it'll give me a result based on these numbers. When the numbers are just numbers (e.g., 12 or 15) ...
1 vote
1 answer
128 views
Best way to enable user to input formula without making a security hole?
I would like to enable the user to input a formula for calculation given some parameters. What is the best way to do this without making a security hole? Kind of like this: def generate_bills(): ...