本文整理汇总了Python中codecs.utf_16_le_decode方法的典型用法代码示例。如果您正苦于以下问题:Python codecs.utf_16_le_decode方法的具体用法?Python codecs.utf_16_le_decode怎么用?Python codecs.utf_16_le_decode使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类codecs
的用法示例。
在下文中一共展示了codecs.utf_16_le_decode方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_errors
# 需要导入模块: import codecs [as 别名]
# 或者: from codecs import utf_16_le_decode [as 别名]
def test_errors(self):
tests = [
(b'\xff', u'\ufffd'),
(b'A\x00Z', u'A\ufffd'),
(b'A\x00B\x00C\x00D\x00Z', u'ABCD\ufffd'),
(b'\x00\xd8', u'\ufffd'),
(b'\x00\xd8A', u'\ufffd'),
(b'\x00\xd8A\x00', u'\ufffdA'),
(b'\x00\xdcA\x00', u'\ufffdA'),
]
for raw, expected in tests:
try:
with self.assertRaises(UnicodeDecodeError):
codecs.utf_16_le_decode(raw, 'strict', True)
self.assertEqual(raw.decode('utf-16le', 'replace'), expected)
except:
print 'raw=%r' % raw
raise
示例2: test_errors
# 需要导入模块: import codecs [as 别名]
# 或者: from codecs import utf_16_le_decode [as 别名]
def test_errors(self):
tests = [
(b'\xff', '\ufffd'),
(b'A\x00Z', 'A\ufffd'),
(b'A\x00B\x00C\x00D\x00Z', 'ABCD\ufffd'),
(b'\x00\xd8', '\ufffd'),
(b'\x00\xd8A', '\ufffd'),
(b'\x00\xd8A\x00', '\ufffdA'),
(b'\x00\xdcA\x00', '\ufffdA'),
]
if sys.implementation.name == 'ironpython':
# IronPython's strings are in the UTF-16 form thus decoding
# a surrogate pair always results in two characters. Consequently,
# decoding a broken surrogate pair results in TWO U+FFFD
tests[4] = (b'\x00\xd8A', '\ufffd\ufffd')
for raw, expected in tests:
self.assertRaises(UnicodeDecodeError, codecs.utf_16_le_decode,
raw, 'strict', True)
self.assertEqual(raw.decode('utf-16le', 'replace'), expected)
示例3: determine_encoding
# 需要导入模块: import codecs [as 别名]
# 或者: from codecs import utf_16_le_decode [as 别名]
def determine_encoding(self):
while not self.eof and len(self.raw_buffer) < 2:
self.update_raw()
if not isinstance(self.raw_buffer, unicode):
if self.raw_buffer.startswith(codecs.BOM_UTF16_LE):
self.raw_decode = codecs.utf_16_le_decode
self.encoding = 'utf-16-le'
elif self.raw_buffer.startswith(codecs.BOM_UTF16_BE):
self.raw_decode = codecs.utf_16_be_decode
self.encoding = 'utf-16-be'
else:
self.raw_decode = codecs.utf_8_decode
self.encoding = 'utf-8'
self.update(1)
示例4: determine_encoding
# 需要导入模块: import codecs [as 别名]
# 或者: from codecs import utf_16_le_decode [as 别名]
def determine_encoding(self):
while not self.eof and (self.raw_buffer is None or len(self.raw_buffer) < 2):
self.update_raw()
if isinstance(self.raw_buffer, bytes):
if self.raw_buffer.startswith(codecs.BOM_UTF16_LE):
self.raw_decode = codecs.utf_16_le_decode
self.encoding = 'utf-16-le'
elif self.raw_buffer.startswith(codecs.BOM_UTF16_BE):
self.raw_decode = codecs.utf_16_be_decode
self.encoding = 'utf-16-be'
else:
self.raw_decode = codecs.utf_8_decode
self.encoding = 'utf-8'
self.update(1)
示例5: decode
# 需要导入模块: import codecs [as 别名]
# 或者: from codecs import utf_16_le_decode [as 别名]
def decode(input, errors='strict'):
return codecs.utf_16_le_decode(input, errors, True)
示例6: _buffer_decode
# 需要导入模块: import codecs [as 别名]
# 或者: from codecs import utf_16_le_decode [as 别名]
def _buffer_decode(self, input, errors, final):
if self.decoder is None:
(output, consumed, byteorder) = \
codecs.utf_16_ex_decode(input, errors, 0, final)
if byteorder == -1:
self.decoder = codecs.utf_16_le_decode
elif byteorder == 1:
self.decoder = codecs.utf_16_be_decode
elif consumed >= 2:
raise UnicodeError("UTF-16 stream does not start with BOM")
return (output, consumed)
return self.decoder(input, self.errors, final)
示例7: decode
# 需要导入模块: import codecs [as 别名]
# 或者: from codecs import utf_16_le_decode [as 别名]
def decode(self, input, errors='strict'):
(object, consumed, byteorder) = \
codecs.utf_16_ex_decode(input, errors, 0, False)
if byteorder == -1:
self.decode = codecs.utf_16_le_decode
elif byteorder == 1:
self.decode = codecs.utf_16_be_decode
elif consumed>=2:
raise UnicodeError,"UTF-16 stream does not start with BOM"
return (object, consumed)
### encodings module API
示例8: test_utf_16_le_decode
# 需要导入模块: import codecs [as 别名]
# 或者: from codecs import utf_16_le_decode [as 别名]
def test_utf_16_le_decode(self):
#sanity
new_str, size = codecs.utf_16_le_decode("abc")
self.assertEqual(new_str, u'\u6261')
self.assertEqual(size, 2)
示例9: setstate
# 需要导入模块: import codecs [as 别名]
# 或者: from codecs import utf_16_le_decode [as 别名]
def setstate(self, state):
# state[1] will be ignored by BufferedIncrementalDecoder.setstate()
codecs.BufferedIncrementalDecoder.setstate(self, state)
state = state[1]
if state == 0:
self.decoder = (codecs.utf_16_be_decode
if sys.byteorder == "big"
else codecs.utf_16_le_decode)
elif state == 1:
self.decoder = (codecs.utf_16_le_decode
if sys.byteorder == "big"
else codecs.utf_16_be_decode)
else:
self.decoder = None
示例10: decode
# 需要导入模块: import codecs [as 别名]
# 或者: from codecs import utf_16_le_decode [as 别名]
def decode(self, input, errors='strict'):
(object, consumed, byteorder) = \
codecs.utf_16_ex_decode(input, errors, 0, False)
if byteorder == -1:
self.decode = codecs.utf_16_le_decode
elif byteorder == 1:
self.decode = codecs.utf_16_be_decode
elif consumed>=2:
raise UnicodeError("UTF-16 stream does not start with BOM")
return (object, consumed)
### encodings module API
示例11: test_errors
# 需要导入模块: import codecs [as 别名]
# 或者: from codecs import utf_16_le_decode [as 别名]
def test_errors(self):
tests = [
(b'\xff', u'\ufffd'),
(b'A\x00Z', u'A\ufffd'),
(b'A\x00B\x00C\x00D\x00Z', u'ABCD\ufffd'),
(b'\x00\xd8', u'\ufffd'),
(b'\x00\xd8A', u'\ufffd'),
(b'\x00\xd8A\x00', u'\ufffdA'),
(b'\x00\xdcA\x00', u'\ufffdA'),
]
for raw, expected in tests:
self.assertRaises(UnicodeDecodeError, codecs.utf_16_le_decode,
raw, 'strict', True)
self.assertEqual(raw.decode('utf-16le', 'replace'), expected)