python:从字符串中获取dict对象

时间:2016-03-22 16:19:25

标签: python string dictionary get

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

<services>
  <!-- The namespace appears in the 'name' attribute -->
  <service name="Your.Namespace.ConcreteClassName">
    <endpoint address="http://localhost/YourService.svc"
      binding="basicHttpBinding" bindingConfiguration="NewBinding1"
      contract="Your.Namespace.IConcreteClassName" />
  </service>
</services>

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

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

有解决方案吗?

1 个答案:

答案 0 :(得分:0)

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