How to eval() a string in a dictionary Python2.7
<p>I have a dictionary:</p>

<pre><code>key = {'K_z':'K_w'}
</code></pre>

<p>I want to get K_w as a value name instead of a string, I heard that the eval() function can convert strings to value names.</p>

<p>But when I type:</p>

<pre><code>print(eval(key['K_z']))
</code></pre>

<p>I get 'K_z' instead of having the K_w value.</p>

<p>Can you help me ?</p>

<p>If it helps, I'm using it for pygame to let the player change the key bindings, specially with AZERTY and QWERTY differences...</p>

<pre><code>import pygame
key = {'K_z':'K_w',}    
print (getattr(pygame,key['K_z']))
</code></pre>

<p>Using this <a href="http://claude.segeral.pagesperso-orange.fr/qwerazer/" rel="nofollow">azerty and qwerty </a> you can easily create the key dictionary by copying and editing  the html table into excel and then some scripts </p>

python dictionary pygame eval