Reputation: 1621
I am following this post
https://stackoverflow.com/a/9016545
and i want to know that how can i do that in Python. I don't know how can i insert BOM data in there
This is my current code
response = HttpResponse(content_type='text/csv')
response['Content-Type'] = 'application/octet-stream'
response['Content-Disposition'] = 'attachment; filename="results.csv"'
writer = UnicodeWriter(response, quoting=csv.QUOTE_ALL, encoding="utf-8")
I want to convert to utf -16 . BOm data is this but don't know how to insert it From here https://stackoverflow.com/a/4440143
echo "\xEF\xBB\xBF"; // UTF-8 BOM
But i want it for python and utf-16
I tried opening that csv in notepad and insert \xef\xbb\xb
in beginning and excel displayed that correctly. But it is also visible before first column.
How can i hide that because user wont like that
Upvotes: 0
Views: 1380
Reputation: 177554
Either of these lines write the correct BOM for the encoding. If the BOM is correct, Excel should not display it.
writer = UnicodeWriter(response, quoting=csv.QUOTE_ALL, encoding="utf-8-sig")
or:
writer = UnicodeWriter(response, quoting=csv.QUOTE_ALL, encoding="utf16")
utf8
, utf-16le
and utf-16be
do not write a BOM.
Upvotes: 2
Reputation: 785
I think you are inserting the wrong BOM. From https://en.wikipedia.org/wiki/Byte_order_mark#UTF-16 it suggests you should use FF FE or FE FF (depending upon big or little endian)
The python docs (at http://docs.python.org/2/howto/unicode.html) suggest there is an encoding setting for utf-16. In addition 'there are variants of these encodings, such as ‘utf-16-le’ and ‘utf-16-be’ for little-endian and big-endian encodings'. Perhaps these will allow the BOM to be inserted automatically during writing.
Upvotes: 0