Python 3:读取UCS-2(BE)文件 [英] Python 3: reading UCS-2 (BE) file
问题描述
在Python 3.3下,我似乎无法使用内置的open()函数(堆栈跟踪显示UnicodeDecodeError并包含我的readLine()方法)解码UCS-2 BE文件(旧版内容)-在实际上,我找不到用于指定此编码的标志.
I can't seem to be able to decode UCS-2 BE files (legacy stuff) under Python 3.3, using the built-in open() function (stack trace shows UnicodeDecodeError and contains my readLine() method) - in fact, I wasn't able to find a flag for specifying this encoding.
使用Windows 8,使用"Lucida Console"字体将终端设置为代码页65001.
Using Windows 8, terminal is set to codepage 65001, using 'Lucida Console' fonts.
我想代码片段不会有太大帮助:
Code snippet won't be of too much help, I guess:
def display_resource():
f = open(r'D:\workspace\resources\JP.res', encoding=<??tried_several??>)
while True:
line = f.readline()
if len(line) == 0:
break
感谢您对该问题的任何见识.
Appreciating any insight into this issue.
推荐答案
UCS-2是UTF- 16 实际上,对于在任何情况下仍称为UCS-2时分配的任何代码点.
UCS-2 is UTF-16, really, for any codepoint that was assigned when it was still called UCS-2 in any case.
用encoding='utf16'
打开它.如果没有BOM(字节顺序标记,开头为2个字节,表示BE), d为\xfe\xff
),然后使用encoding='utf_16_be'
强制执行字节顺序.
Open it with encoding='utf16'
. If there is no BOM (the Byte order mark, 2 bytes at the start, for BE that'd be \xfe\xff
), then use encoding='utf_16_be'
to force a byte order.
这篇关于Python 3:读取UCS-2(BE)文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!