title: How can I replace'&' to '&amp;' in python?
body: <p>I'm having issues with <code>.replace()</code>.  My XML parser does not like '&amp;', but will accept '&amp;\amp;'.  I'd like to use <code>.replace('&amp;','&amp;amp;')</code> but this does not seem to be working.  I keep getting the error:</p>
  
  <pre><code>lxml.etree.XMLSyntaxError: xmlParseEntityRef: no name, line 51, column 41
  </code></pre>
  
  <p>So far I have tried just a straight forward <code>file=file.replace('&amp;','&amp;amp;')</code>, but this doesn't work.  I've also tried:</p>
  
  <pre><code>xml_file = infile
  file=xml_file.readlines()
  for line in file:
          for char in line:
                  char.replace('&amp;','&amp;amp;')
  infile=open('a','w')
  file='\n'.join(file)
  infile.write(file)
  infile.close()
  infile=open('a','r')
  xml_file=infile
  </code></pre>
  
  <p>What would be the best way to fix my issue?</p>