title: StringIO generated csv file that includes BOM
body: <p>I'm trying to generate a CSV that opens correctly in Excel, but using StringIO instead of a file.</p>
  
  <pre><code>  output = StringIO("\xef\xbb\xbf") # Tried just writing a BOM here, didn't work
    fieldnames = ['id', 'value']
    writer = csv.DictWriter(output, fieldnames, dialect='excel')
    writer.writeheader()
    for d in Data.objects.all():
          writer.writerow({
            'id': d.id,
            'value': d.value
          })
    response = HttpResponse(output.getvalue(), content_type='text/csv')
    response['Content-Disposition'] = 'attachment; filename=data.csv')
    return response
  </code></pre>
  
  <p>This is part of a Django view, so I'd really rather not get into the business of dumping out temporary files for this. </p>
  
  <p>I've also tried the following:</p>
  
  <pre><code>response = HttpResponse(output.getvalue().encode('utf-8').decode('utf-8-sig'), content_type='text/csv')
  </code></pre>
  
  <p>with no luck</p>
  
  <p>What can I do to get the output file correctly encoded in <code>utf-8-sig</code>, with a BOM, so that Excel will open the file and show multi-byte unicode characters correctly?</p>