【问题标题】:python:get dict object from a stringpython:从字符串中获取dict对象
【发布时间】:2016-03-22 16:19:24
【问题描述】:

我已经从一个列表中创建了 dict 动态:

exec("mydict_%=%" % (key, value))

我尝试从如下字符串中获取对象:

def getdict(String):
   code ...
   return dict


mydict = []
mydict = getdict("mydict_sample")

你有解决办法吗?

【问题讨论】:

  • 你应该使用一个列表而不是定义多个变量。

标签: python string dictionary get


【解决方案1】:

ast.literal_eval 可以评估代表 python dicts 的字符串。见the documentation

【讨论】:

  • 非常感谢:安全地评估表达式节点或字符串
猜你喜欢
  • 2021-05-04
  • 1970-01-01
  • 2014-02-04
  • 2013-07-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-04
  • 2013-06-06
相关资源
最近更新 更多