0

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

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

ok so u mean i dont need to write echo "\xEF\xBB\xBF"; any where in the BOM
where can i find more info about BOM . i could not find any where
I have seen that it works on PC and Excel but its not working on MAx Excel 2011. i am using 8-sig. Do u know which thing can work in Excel for mac
Try without the BOM on Mac.
0

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.

1 Comment

i want to know , how to insert BOM in python

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.