0

I have this exact problem: https://www.en.adwords-community.com/t5/Basics-for-New-Advertisers/Character-Encoding-used-by-the-editor/td-p/100244

which can apparently be solved by writing a byte order mark first to the file. But how do I do this with xlwt? Can I just write it to the first cell of the first worksheet? Or do I need to like write the file and then append it to a new file that's just a byte order mark? I can't find any resources on this.

Colleen
  • 18,089
  • 12
  • 42
  • 70
  • 3
    `xlwt` doesn't produce CSV files. Writing a byte-order mark *only works for CSV files* (and [more recent versions of Excel](http://stackoverflow.com/a/11399444/100297)). – Martijn Pieters Jun 06 '14 at 21:09
  • hey now... that's probably why we're not getting the right encoding. This code makes a workbook and then changes the extension type at the end. I didn't write it lol! However, now that you've prompted me to look closer I see we use csv.writer and I can probably just write to that with the BOM before writing the workbook content to it... weird code... – Colleen Jun 06 '14 at 21:12
  • err, can I? csv.writer doesn't have a write method... – Colleen Jun 06 '14 at 21:14
  • Python 3: [Writing to excel string in encoding UTF-16](http://stackoverflow.com/a/18242707), Python 2: [Google App engine python writing csv in arabic](http://stackoverflow.com/a/23298815) – Martijn Pieters Jun 06 '14 at 21:14
  • E.g. write the BOM to the file object used for the `csv.writer()` directly, before using the `csv.writer()` object itself. – Martijn Pieters Jun 06 '14 at 21:16
  • hmmm... hopefully that will work if there is no actual *file*, but just a `StringIO` object? – Colleen Jun 06 '14 at 21:17
  • Sure, there is no difference there between an actual file object and an in-memory object. – Martijn Pieters Jun 06 '14 at 21:20
  • yeah, I guess my concern was less about the object type and more about what workbook.save() would do with the object... I thought `output = StringIO.StringIO() output.write('\xEF\xBB\xBF') workbook.save(output)` might work but I'm getting a new kind of error with that – Colleen Jun 06 '14 at 21:29
  • 1
    Note again: `xlwt` *does not produce CSV* and the BOM option *does not apply*. – Martijn Pieters Jun 06 '14 at 21:37
  • `xlwt` instead [supports Unicode](http://stackoverflow.com/questions/7184454/has-anyone-been-able-to-write-out-utf-8-characters-using-pythons-xlwt). – Martijn Pieters Jun 06 '14 at 21:38
  • oh, I didn't realize BOM was not an option for xls-ish files, thanks! – Colleen Jun 06 '14 at 21:38
  • @MartijnPieters any idea why `response = HttpResponse(content_type='text/csv') response['Content-Disposition'] = 'attachment; filename='+(account.name+'-'+spreadsheet).replace(',', '')+'.csv' response.write('\xEF\xBB\xBF') writer = csv.writer(response)` doesn't work? – Colleen Jun 06 '14 at 22:06
  • @MartijnPieters neither does `csvoutput.append(problematicstring.decode('utf-8'))` – Colleen Jun 06 '14 at 22:07
  • Nope, no idea. That's usually the case for *doesn't work* as a problem description, however. – Martijn Pieters Jun 06 '14 at 22:07
  • I can't parse your second sentence. – Colleen Jun 06 '14 at 22:08
  • It means I don't do guesswork all that well. You just said that *it doesn't work*. That is not a detailed enough problem description. Also, helping out people in comments doesn't really work either, I am afraid. – Martijn Pieters Jun 06 '14 at 22:11

0 Answers0