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

4

2 回答 2

2

这些行中的任何一行都为编码编写了正确的 BOM。如果 BOM 正确,Excel 不应显示它。

writer = UnicodeWriter(response, quoting=csv.QUOTE_ALL, encoding="utf-8-sig")

或者:

writer = UnicodeWriter(response, quoting=csv.QUOTE_ALL, encoding="utf16")

utf8,utf-16le并且utf-16be 不要写 BOM。

于 2013-06-26T04:32:11.693 回答
0

我认为您插入了错误的 BOM。从https://en.wikipedia.org/wiki/Byte_order_mark#UTF-16它建议您应该使用 FF FE 或 FE FF(取决于大端或小端)

python 文档(位于http://docs.python.org/2/howto/unicode.html)建议有 utf-16 的编码设置。此外,“这些编码还有一些变体,例如用于小端和大端编码的 'utf-16-le' 和 'utf-16-be'”。也许这些将允许在写入过程中自动插入 BOM。

于 2013-06-26T04:18:47.773 回答