将字符串转换为命令.. [英] Convert string to command..

查看:54
本文介绍了将字符串转换为命令..的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将字符串转换为命令..

例如我有一个字符串:

a =" [''1'']"

我想做这个清单..

我该怎么办?

解决方案

< blockquote> 10月18日上午10点23分,放弃< best ... @ gmail.comwrote:


我想将字符串转换为命令..

例如我有一个字符串:

a =" [''1'']"

我想做这个列表..

我该怎么办?



使用内置函数eval。


Abandoned写道:


我想将字符串转换为命令..

例如我有一个字符串:

a =" [ ''1'']"

我想做这个清单..

我该怎么办?



这里的正确措辞是表达式。为了评估表达式,

是函数eval:


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


但要注意:如果表达式包含一些可能有害的代码,那么将执行
。因此,使用eval通常被认为是不好的风格。


失败的一个例子是


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


或类似的事情。


另一种选择是使用像simplejson这样的解析器来提取

信息。这当然只有你的表达式是有效的json才有效。


Diez


谢谢大家回答..

但是eval非常大的字典非常慢{2:3,4:5,6:19 ....}

(100.000元素)

有没有简单的替代方案?


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 ?

解决方案

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".


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


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 ?


这篇关于将字符串转换为命令..的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆