简体   繁体   中英

encode 'UCS-2 Little Endian' file to 'utf8' using python error

I'm trying to encode from UCS-2 Little Endian file to utf8 using python and I'm getting a weird error.

The code I'm using:

file=open("C:/AAS01.txt", 'r', encoding='utf8')
lines = file.readlines()
file.close()

And I'm getting the following error:

Traceback (most recent call last):
  File "C:/Users/PycharmProjects/test.py", line 18, in <module>
    main()
  File "C:/Users/PycharmProjects/test.py", line 7, in main
    lines = file.readlines()
  File "C:\Python34\lib\codecs.py", line 319, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte

I tried to use codecs commands, but also didn't work... Any idea what I can do?

The encoding argument to open sets the input encoding. Use encoding='utf_16_le' .

If you're trying to read UCS-2, why are you telling Python it's UTF-8? The 0xff is most likely the first byte of a little endian byte order marker:

>>> codecs.BOM_UTF16_LE
b'\xff\xfe'

UCS-2 is also deprecated, for the simple reason that Unicode outgrew it. The typical replacement would be UTF-16.

More info linked in Python 3: reading UCS-2 (BE) file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM