python - 使用python错误将 'UCS-2 Little Endian'文件编码为 'utf8'

标签 python file utf-8 decode

我正在尝试使用 python 将 UCS-2 Little Endian 文件编码为 utf8,但我遇到了一个奇怪的错误。

我使用的代码:

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

我收到以下错误:

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

我尝试使用编解码器命令,但也没有用... 知道我能做什么吗?

最佳答案

openencoding 参数设置输入 编码。使用 encoding='utf s16_le'

关于python - 使用python错误将 'UCS-2 Little Endian'文件编码为 'utf8',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45393818/

相关文章:

c++ - 用C++读取二进制数据并写入MySQL

javascript - jquery 如何检测文件输入元素的更改?

perl - 如何在 Perl 中使用变量的值作为 glob 模式?

ios - 当文件名包含 unicode(utf8) 字符时,ios 10.3 中的 fopen 出现问题

python - MySql 没有正确比较 utf-8 字符串?

python - 如何使用 Python OpenCV 检测和查找表单中的复选框?

python - 将字符串的 Pandas DataFrame 转换为直方图

python - nidaqmx.任务 : (OSError: [WinError 126] The specified module could not be found)

python - 获取 DataFrame 的不同输出和相同代码的正常实现

php - DOCX 编码问题