title: xml excape form a python dictionary
body: <p>I have a list of dictionaries and want to iterate through the keys in order to escape xml characters. I need to find strings containing characters needing xml escaping, update them and add them back into the list of dictionaries.</p>
  
  <p>There are two keys which needs to be checked and updated 'name' and 'name1'.</p>
  
  <p>My list looks like this:</p>
  
  <pre><code>list: [{'level1': 3, 'code1': u'7000', 'name1': u'Donations received in the field', 'code': u'6100', 'name': u'Per diem', 'level': 3, 'balance': 0.0, 'balance1': -1986.9100000000001}, {'level1': 3, 'code1': u'7100', 'name1': u'Interest income', 'code': u'6101', 'name': u'Team living Expenses', 'level': 3, 'balance': 0.0, 'balance1': 0.0}, etc ... ] 
  </code></pre>
  
  <p>My code so far, but the 'key' variable is returning a integer and the 'val' holds the key... (eg: 'level1') and it never makes it into the reduce lambda.</p>
  
  <pre><code>def get_lines(self,data):
      if data['form']['export_format'] == 'xls':
          for row in self.result_temp:
              for key,val in row.items():
                  if key == 'name' or key == 'name1':
                      reduce(lambda s,r: s.replace(*r),
                          [('&amp;', '&amp;amp;'),
                          ('&lt;', '&amp;lt;'),
                          ('&gt;', '&amp;gt;'),
                          ("'", '&amp;#39;'),
                          ('"', '&amp;quot;')],
                          self.result_temp[row][val])
      return self.result_temp
  </code></pre>