title: 'utf-8' codec can't decode byte 0xa0 in position 0: invalid start byte django
body: <p>so i have variable contains in bytes and i want to save it to str. but how to do it? i tried various kinds of ways, but always got error</p>
  <blockquote>
  <p>'utf-8' codec can't decode byte 0xa0 in position 0: invalid start byte</p>
  </blockquote>
  <p>i want to save the result encrypt file to text file. so i have the proof it the file was encrypt. here;s my ways 1:</p>
  <pre><code> def create_file(f):
       response = HttpResponse(content_type=&quot;text/plain&quot;)
       response['Content-Disposition'] = 'attachment; filename=file.txt'
  
       filename = f
       print(filename)
       name_byte = codecs.decode(filename)
       print(name_byte)
  
       return response
  </code></pre>
  <p>my ways 2 :</p>
  <pre><code>def create_file(enc):
      with open(&quot;file.txt&quot;, &quot;w&quot;, encoding=&quot;utf-8&quot;) as f:
          enc = enc.decode('utf-8')
          f.write(enc)
  </code></pre>
  <p>my ways 3:</p>
  <pre><code>def create_file(f):
      file = open(&quot;file.txt&quot;, &quot;w&quot;)
      f = f.decode('utf-8')
      download = file.write(f)
      file.close()
      print(download)
  
      return download
  </code></pre>
  <p>f = b'\xa0\x0e\xdc\x14'
  f is the return result of encrypt</p>
  <p>i called the function :</p>
  <pre><code>#in views
  download = create_file(enc)
             print(download)
  
  #in urls
  path(&quot;create_file&quot;, views.create_file, name=&quot;create_file&quot;),
  
  #in html
  &lt;a href=&quot;{% url 'create_file' %}&quot;&gt;
  </code></pre>