4

I have a project in which most of the files are UTF-16 but one is UTF-8. Having put the correct encoding ("utf_8" or "utf_16") into strOpenEncoding, I tried this:

for strInput in open(strInputFileName, "r", newline="\n", encoding=strOpenEncoding):

I get this response:

UnicodeError: UTF-16 stream does not start with BOM

I have opened the file it is trying to open manually, and it definitely contains a byte-order mark and is definitely UTF-16. What am I doing wrong?

Stephen
  • 61
  • 1
  • 6
  • Can you copy the file contents into an inline test case, e.g. `encodings.utf_16.IncrementalDecoder().decode(b'\x12\x34')`? – Josh Lee Mar 27 '17 at 15:30
  • Just check http://stackoverflow.com/questions/6779315/utf-16-file-seeking-in-python-how if this of any help to you. – Len Mar 27 '17 at 15:36
  • 1
    Take no notice. I was passing the wrong file name to the open file routine, and it really was trying to open a UTF-8 file. Hence the error message. I apologise for wasting your time. Thanks to those people who answered so quickly. – Stephen Mar 27 '17 at 16:32

0 Answers0