| | |
Converting string to python code
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
Hi all. Is there any way that if I give python a string, it can convert it into code? Example, say I am given this string:
Now I want to convert it into code:
Any ideas?
Thanks.
python Syntax (Toggle Plain Text)
string = "Class( 0,100 )"
Now I want to convert it into code:
python Syntax (Toggle Plain Text)
Class( 0,100 )
Any ideas?
Thanks.
How are you giving the strings?
If you are giving a complete python program in the string, you do a trick:
Write all the string into a file and execute it with the interpreter by using a execv or system command.
If you are giving a complete python program in the string, you do a trick:
Write all the string into a file and execute it with the interpreter by using a execv or system command.
Siddhant Sanyam
(Not posting much)
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
(Not posting much)
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
Please know that this is a security risk. Use the exec function:
python Syntax (Toggle Plain Text)
>>> exec("print('df')") df
Hate to bother you, but I have one more question. I have this string:
and when I do exec:
python doesn't return anything (like init values that have been printed and returned statements). What could be the problem?
python Syntax (Toggle Plain Text)
temp_string = "Vent( Screen, 100, 484, 'Vent' )"
python Syntax (Toggle Plain Text)
exec(temp_string)
•
•
Join Date: Dec 2006
Posts: 1,022
Reputation:
Solved Threads: 287
What does it return, an error message? exec() will work on things like print statements or 1+2-3, but if you call a function or class then you must already have that function defined, otherwise an error message. Perhaps if you explained what you are trying to do, there would be an easier way, like a generic class that you would pass the strings in the file to as parameters --> some_class( the_string ).
Linux counter #99383
Well, I have a class imported from a file:
in the main program, I read a text file which outputs:
I assign that string from the text file to a var called 'temp_string', and then I go:
It doesn't work, but I write up some code that (based of the strings from the text file) recognizes the first four characters in a line, a.k.a 'Vent', and then gets the string(s) from in between the characters '(' and ',' and then ',' and ','. The last argument just stays a string. I then pass a new instance with those new given values. It's no big deal if the exec thing doesn't work, but thanks any ways
python Syntax (Toggle Plain Text)
from lib.class import *
python Syntax (Toggle Plain Text)
Vent( 100,300, 'Vent' )
python Syntax (Toggle Plain Text)
exec(my_string)
![]() |
Similar Threads
- Display Python code on Blogspot (Python)
- python code security or set up a complied python code with a password (Python)
- Converting String to Long (Java)
- converting string[][] to float[] (C)
- Help with Python code and accessing ZODB (Python)
- String to Int (C++)
- Converting a String into an integer array (C)
- Converting String to Integer help (C++)
Other Threads in the Python Forum
- Previous Thread: Should I use a global variable (array in this case) or should i return the array
- Next Thread: wxpython help
| Thread Tools | Search this Thread |
accessdenied advanced apache application argv beginner book change command csv def dictionary dynamic edit enter event examples file float format function google gui homework import inches input jaunty java keyboard lapse library line lines linux list lists loop microphone mouse movingimageswithpygame mysql newb number numbers numeric obexftp output parameters parsing path phonebook port prime programming projects py2exe pygame pygtk pyopengl pysimplewizard python random recursion redirect remote return reverse scrolledtext session simple skinning smtp software sprite statictext stderr string strings syntax table tennis terminal text thread threading time tlapse trick tuple tutorial ubuntu unicode unit urllib urllib2 variable voip wordgame wxpython






