title: Python space in xml element text when using "&"
body: <p>I have a script which pulls XML hosted online and saves it locally. The script then goes through the local file and replaces/adds certain text. However, for some reason, when I use the "&amp;" symbol, there is an extra space inserted along with it within the element text. Here is a sample of the XML elements I am parsing:</p>
  
  <pre><code>&lt;TrackingEvents&gt;
  &lt;Tracking event="rewind"&gt;
  http://www.example.com/rewind_1.png?test=rewind_test 
  &lt;/Tracking&gt;
  &lt;Tracking event="pause"&gt;
  http://www.example.com/pause_1.png?test=rewind_test 
  &lt;/Tracking&gt;
  </code></pre>
  
  <p>However, after running my script to add the additional test to my elements, the text is added with an additional space, like this:</p>
  
  <pre><code>&lt;TrackingEvents&gt;
  &lt;Tracking event="rewind"&gt;
  http://www.example.com/rewind_1.png?test=rewind_test &amp;cb={CACHEBUSTER}
  &lt;/Tracking&gt;
  &lt;Tracking event="pause"&gt;
  http://www.example.com/pause_1.png?test=rewind_test &amp;cb={CACHEBUSTER}
  &lt;/Tracking&gt;
  </code></pre>
  
  <p>I have tried everything but I don't know why this is occurring or what I can do to prevent this space from being added. I have even tried to strip the white space as well. When I look at the XML that is saved locally before uploading it, everything looks fine (&amp; is for the "&amp;" symbol) as seen here from the source:</p>
  
  <pre><code>&lt;Tracking event="rewind"&gt;
  http://www.example.com/rewind_1.png?test=rewind_test
  &amp;amp;cb={CACHEBUSTER}&lt;/Tracking&gt;
                          &lt;Tracking event="pause"&gt;
  http://www.example.com/pause_1.png?test=rewind_test
  &amp;amp;cb={CACHEBUSTER}&lt;/Tracking&gt;
  </code></pre>
  
  <p>Here is what the code from my script looks like:</p>
  
  <pre><code>for URL, xml_name, original_server in tqdm(XML_tags):
      response = requests.get(URL)
      with open(xml_name, 'wb') as file:
          file.write(response.content)
      with open(xml_name) as saved_file:
      tree = ET.parse(saved_file)
      root = tree.getroot()
  
      for element in root.iter(tag=ET.Element):
      if element.text != None:
          if ".png" in element.text:
              if "?" in element.text:
                  element.text = element.text + "&amp;cb={CACHEBUSTER}"
                  element.text = element.text.strip()
              else:
                  element.text = element.text + "?cb={CACHEBUSTER}"
                  element.text = element.text.strip()
          else:
              pass
  
      server = "example.server: ../sample/sample/" + original_server
      tree.write(xml_name, xml_declaration=True, method='xml', 
      encoding='utf8')
      server_upload = subprocess.Popen(["scp", xml_name, server])
      upload_wait = os.waitpid(server_upload.pid, 0)
  </code></pre>
  
  <p>I can definitely use some help with this. Thanks.</p>
  
  <p>Update: Actually, it appears that this has nothing to do with using the "&amp;". Here is a sample when I just add different text:</p>
  
  <pre><code>&lt;TrackingEvents&gt;
  &lt;Tracking event="rewind"&gt;
  http://www.example.com/rewind_1.png?test=rewind_test test123
  &lt;/Tracking&gt;
  &lt;Tracking event="pause"&gt;
  http://www.example.com/pause_1.png?test=rewind_test test123
  &lt;/Tracking&gt;
  &lt;/TrackingEvents&gt;
  </code></pre>