From d78e4807c3b51fcef7508df2678b13b9c1437f81 Mon Sep 17 00:00:00 2001 From: Eli Schwartz Date: Mon, 9 Sep 2019 20:38:25 -0400 Subject: [PATCH] py3: write unicode bom without using bytes() type MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Since the file is opened in utf-8 mode, it needs to be written to using unicode text, not a b'' string. In python3, '\xef\xbb\xbf' becomes '' which is definitely not a BOM. Writing the unicode escaped codepoint allows the codecs.open() encoding to correctly choose the right bytes for the BOM and insert it as needed. --- src/calibre/library/catalogs/csv_xml.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/calibre/library/catalogs/csv_xml.py b/src/calibre/library/catalogs/csv_xml.py index bed17ff771..8576e3849b 100644 --- a/src/calibre/library/catalogs/csv_xml.py +++ b/src/calibre/library/catalogs/csv_xml.py @@ -107,7 +107,7 @@ class CSV_XML(CatalogPlugin): outfile = codecs.open(path_to_output, 'w', 'utf8') # Write a UTF-8 BOM - outfile.write('\xef\xbb\xbf') + outfile.write(u'\ufeff') # Output the field headers outfile.write(u'%s\n' % u','.join(fields))