473,785 Members | 2,419 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Convert string to command..

I want to convert a string to command..
For example i have a string:
a="['1']"
I want to do this list..
How can i do ?

Oct 18 '07 #1
35 4926
On Oct 18, 10:23 am, Abandoned <best...@gmail. comwrote:
I want to convert a string to command..
For example i have a string:
a="['1']"
I want to do this list..
How can i do ?
Use the builtin function "eval".

Oct 18 '07 #2
Abandoned wrote:
I want to convert a string to command..
For example i have a string:
a="['1']"
I want to do this list..
How can i do ?
The correct wording here would be expression. To evaluate expressions, there
is the function eval:

a = eval("['1']")

But beware: if the expression contains some potentially harmful code, it
will be executed. So it is generally considered bad style to use eval.

An example that fails would be

a = eval("10**2000* *2000")

or such thing.

Another alternative is to use parsers like simplejson to extract the
information. This of course only works, if your expressions are valid json.

Diez
Oct 18 '07 #3
Thanks you all answer..
But "eval" is very slow at very big dictionary {2:3,4:5,6:19.. ..}
(100.000 elements)
Is there any easy alternative ?

Oct 18 '07 #4
Abandoned wrote:
Thanks you all answer..
But "eval" is very slow at very big dictionary {2:3,4:5,6:19.. ..}
(100.000 elements)
Is there any easy alternative ?
How big? How slow? For me, a 10000-element list takes 0.04 seconds to be
parsed. Which I find fast.

Diez
Oct 18 '07 #5
On Oct 18, 6:14 pm, "Diez B. Roggisch" <de...@nospam.w eb.dewrote:
Abandoned wrote:
Thanks you all answer..
But "eval" is very slow at very big dictionary {2:3,4:5,6:19.. ..}
(100.000 elements)
Is there any easy alternative ?

How big? How slow? For me, a 10000-element list takes 0.04 seconds to be
parsed. Which I find fast.

Diez
173.000 dict elements and it tooks 2.2 seconds this very big time for
my project

Oct 18 '07 #6
Abandoned <be*****@gmail. comwrites:
173.000 dict elements and it tooks 2.2 seconds this very big time
for my project
If you're generating the string from Python, use cPickle instead.
Much faster:
>>import time
d = dict((i, i+1) for i in xrange(170000))
len(d)
170000
>>s=repr(d)
t0 = time.time(); d2 = eval(s); t1 = time.time()
t1-t0
1.5457899570465 088
>>import cPickle as pickle
s = pickle.dumps(d, -1)
len(s)
1437693
>>t0 = time.time(); d2 = pickle.loads(s) ; t1 = time.time()
t1-t0
0.0603079795837 40234
>>len(d2)
170000

That is 25x speedup. Note that cPickle's format is binary. Using the
textual format makes for more readable pickles, but reduces the
speedup to "only" 9.5x on my machine.
P.S.
Before someone says that using pickle is unsafe, remember that he is
considering *eval* as the alternative. :-)
Oct 18 '07 #7
Abandoned wrote:
On Oct 18, 6:14 pm, "Diez B. Roggisch" <de...@nospam.w eb.dewrote:
>Abandoned wrote:
Thanks you all answer..
But "eval" is very slow at very big dictionary {2:3,4:5,6:19.. ..}
(100.000 elements)
Is there any easy alternative ?

How big? How slow? For me, a 10000-element list takes 0.04 seconds to be
parsed. Which I find fast.

Diez

173.000 dict elements and it tooks 2.2 seconds this very big time for
my project
Where does the data come from?

Diez
Oct 18 '07 #8
On Oct 18, 6:26 pm, Hrvoje Niksic <hnik...@xemacs .orgwrote:
Abandoned <best...@gmail. comwrites:
173.000 dict elements and it tooks 2.2 seconds this very big time
for my project

If you're generating the string from Python, use cPickle instead.
Much faster:
>import time
d = dict((i, i+1) for i in xrange(170000))
len(d)
170000
>s=repr(d)
t0 = time.time(); d2 = eval(s); t1 = time.time()
t1-t0
1.5457899570465 088
>import cPickle as pickle
s = pickle.dumps(d, -1)
len(s)
1437693
>t0 = time.time(); d2 = pickle.loads(s) ; t1 = time.time()
t1-t0

0.0603079795837 40234>>len(d2)

170000

That is 25x speedup. Note that cPickle's format is binary. Using the
textual format makes for more readable pickles, but reduces the
speedup to "only" 9.5x on my machine.

P.S.
Before someone says that using pickle is unsafe, remember that he is
considering *eval* as the alternative. :-)

import cPickle as pickle
a="{2:3,4:6,2:7 }"
s=pickle.dumps( a, -1)
g=pickle.loads( s);
print g
'{2:3,4:6,2:7}'

Thank you very much for your answer but result is a string ??

Oct 18 '07 #9
On Oct 18, 6:51 pm, Marc 'BlackJack' Rintsch <bj_...@gmx.net wrote:
On Thu, 18 Oct 2007 08:41:30 -0700, Abandoned wrote:
import cPickle as pickle
a="{2:3,4:6,2:7 }"
s=pickle.dumps( a, -1)
g=pickle.loads( s);
print g
'{2:3,4:6,2:7}'
Thank you very much for your answer but result is a string ??

In Python terms yes, strings in Python can contain any byte value. If you
want to put this into a database you need a BLOB column or encode it as
base64 or something similar more ASCII safe.

Ciao,
Marc 'BlackJack' Rintsch
'{2:3,4:6,2:7}' already in database, i select this and convert to real
dictionary..

Oct 18 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
1755
by: | last post by:
How do I convert a single character, e.g. "a" into char for use in the 'split' command? p.s. I have option strict on Tia.
9
9331
by: ka1cqd | last post by:
I have been looking all over the place for a method to take command line arguments and convert them to a string or wstring so i can process the data and then covert the resulting strings to LPCWSTRs. I have tryed several methods and none work. They include: the convert utilities A2W but that does not compile because of unknow variables declared in the convert header file. MultiByteToWideChar which seems to work once but not the second...
4
118818
by: dba_222 | last post by:
Dear Experts, Ok, I hate to ask such a seemingly dumb question, but I've already spent far too much time on this. More that I would care to admit. In Sql server, how do I simply change a character into a number?????? In Oracle, it is:
4
1895
by: Mike Collins | last post by:
This worked in the command window while in debug mode ?table.Rows.ItemArray.ToString() "f0165f94-648f-4997-b578-11d89c8b1f61" But gives the error below when I compile. Cannot implicitly convert type 'string' to 'int' and the word ColumnName is underlined.
3
13840
by: GM | last post by:
Dear all, Could you all give me some guide on how to convert my big5 string to unicode using python? I already knew that I might use cjkcodecs or python 2.4 but I still don't have idea on what exactly I should do. Please give me some sample code if you could. Thanks a lot Regards, Gary
9
3522
by: keliie | last post by:
Hello (from Access novice), I'm building a switchboard form (using a Treeview object). The treeview is populated by two tables (tblSwitchboardParent and tblSwitchboardChild). Within tblSwitchboardChild, I have a string field called ChildArgument that contains string text of VBA code (e.g., DoCmd.OpenForm "myForm"). When users click on various portions of the Treeview object I want the Tree to either expand or open the report / form.
27
5152
by: comp.lang.tcl | last post by:
My TCL proc, XML_GET_ALL_ELEMENT_ATTRS, is supposed to convert an XML file into a TCL list as follows: attr1 {val1} attr2 {val2} ... attrN {valN} This is the TCL code that does this: set contents ]; close $fileID
4
3211
by: Franky | last post by:
I have a Command Prompt window open and select all the characters and copy them to the clipboard. I then read them from the clipboard str = CType(DataO.GetData(DataFormats.OemText, False), String) and try to convert them to unicode Dim InEncoding As Encoding = Encoding.GetEncoding(437)
0
2880
by: =?Utf-8?B?cm9uZSBtYXRpYXM=?= | last post by:
I have the same task to do but everytime I tried to parse my code I get a null value returned after executing "dtMaterials.WriteXml(swMaterials);". I am using the following code: Hope you can hep me out with this. Thanks. DataTable dtMaterials = new DataTable(); StringWriter swMaterials = new StringWriter(); swMaterials = null; string strMaterials = string.Empty;
3
19872
by: mamul | last post by:
Hi please some one can help me. how to convert char * to string? i have take char *argv from command line and want to pass to a function as string object(string str) i want to first convert argv to string object of type str, then pass to function(). please help me how to convert this
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10325
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10148
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10091
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8972
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6740
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.