title: Reading and writing to text file adds invalid characters
body: <p>Working on reading from, parsing data, and then overwriting new data to a text file. My problem is, writing to the file is adding invalid characters to the beginning before adding what I want it to. The error I get is:</p>
  
  <pre class="lang-none prettyprint-override"><code>ValueError: could not convert string to float: '\x00\x0045.5'
  </code></pre>
  
  <p><code>45.5, 5.0</code> is the data I am trying to write to the file and each <code>\x00</code> represents an invalid character added during the writing to the file process. Normally there's like 10-15 added each iteration. I can manually go into the text file and delete them, but what's the fun in that?</p>
  
  <p>This is my code, and the try/except is what I added to try to fix the problem-</p>
  
  <pre><code>def __init__(self, power):
      self.name = "Dr. Strange"
      self.CloakOfLevitation = "I like Dr. Strange"
      self.EyeOfAgamotto = InfinityStones.TimeStone(5000 + power, self.name)
      self.win = False
      self.length = randint(2, 100)
  
      stats = open("Stats.txt", 'r+', encoding = 'utf-8')
      data = stats.read()
  
      try:
          avg, tries = data.split(sep = ',')
          avg = float(avg)
          tries = float(tries)
      except ValueError:
          stats.truncate(0)
          stats.write(data[1:])
          sleep(5)
          stats.close()
          self.__init__(power)
  
      self.new_avg = avg + (self.length - avg) / tries
      stats.truncate(0)
      stats.write(str(self.new_avg) + ',' + str((tries + 1)))
      stats.close()
  </code></pre>
  
  <p>The majority of this code involves recursion, <code>self.length</code> determines how long the recursion will last, <code>power</code> is just a number and isn't important for this part. <code>sleep(5)</code> is in there so I can stop the program while it's running before it blows up my computer. Each time it writes data to the file, it adds invalid characters (represented as <code>\x00</code> in the error output). <code>avg</code> is supposed to be the average number of 'deaths' before the recursion is done, and <code>tries</code> is the number of times the program has been run.</p>
  
  <p>The idea of the <code>try/except</code> block is to attempt to convert the data in the file to integers so math can be done with them, and if it can't turn it into a character, attempt to drop the first character in the text file (supposedly an invalid character) and then try again (rerun <code>__init__</code>). Currently it just kinda blows up.</p>
  
  <p>I saw stuff about encoding, and tried that, but it didn't change anything. Invalid characters are still being added. I have <code>truncate(0)</code> there to wipe the file so that I'm overwriting the file instead of appending data.</p>