我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用codecs.unicode_escape_decode()。
def main(): start = time() count = 0 items = [] with open(TSV_FILE, 'rb') as fp: reader = csv.reader(fp, delimiter=b'\t') for row in reader: name, h, entity = [v.decode('utf-8') for v in row] s = '\\u{}'.format(h) u = unicode_escape_decode(s)[0] # char = '?' log.info(u) count += 1 items.append(ICON_TPL.format(char=u, info='U+{}'.format(h))) if LIMIT and count == LIMIT: break html = PAGE_TPL.format(content='\n'.join(items), style=STYLE_TPL) with open(HTML_FILE, 'wb') as fp: fp.write(html.encode('utf-8')) log.info('{:d} icons generated in {:0.2f} seconds'.format( count, time() - start))
def decode(self, input, final=False): return codecs.unicode_escape_decode(input, self.errors)[0]
def u(x): return codecs.unicode_escape_decode(x)[0]
def test_unicode_escape(self): # Escape-decoding an unicode string is supported ang gives the same # result as decoding the equivalent ASCII bytes string. self.assertEqual(codecs.unicode_escape_decode(r"\u1234"), ("\u1234", 6)) self.assertEqual(codecs.unicode_escape_decode(br"\u1234"), ("\u1234", 6)) self.assertEqual(codecs.raw_unicode_escape_decode(r"\u1234"), ("\u1234", 6)) self.assertEqual(codecs.raw_unicode_escape_decode(br"\u1234"), ("\u1234", 6))
def test_empty(self): self.assertEqual(codecs.unicode_escape_encode(u""), ("", 0)) self.assertEqual(codecs.unicode_escape_decode(""), (u"", 0))
def test_raw_decode(self): decode = codecs.unicode_escape_decode for b in range(256): if b != ord('\\'): self.assertEqual(decode(chr(b) + '0'), (unichr(b) + u'0', 2))
def test_escape_decode(self): decode = codecs.unicode_escape_decode check = coding_checker(self, decode) check("[\\\n]", u"[]") check(r'[\"]', u'["]') check(r"[\']", u"[']") check(r"[\\]", ur"[\]") check(r"[\a]", u"[\x07]") check(r"[\b]", u"[\x08]") check(r"[\t]", u"[\x09]") check(r"[\n]", u"[\x0a]") check(r"[\v]", u"[\x0b]") check(r"[\f]", u"[\x0c]") check(r"[\r]", u"[\x0d]") check(r"[\7]", u"[\x07]") check(r"[\8]", ur"[\8]") check(r"[\78]", u"[\x078]") check(r"[\41]", u"[!]") check(r"[\418]", u"[!8]") check(r"[\101]", u"[A]") check(r"[\1010]", u"[A0]") check(r"[\x41]", u"[A]") check(r"[\x410]", u"[A0]") check(r"\u20ac", u"\u20ac") check(r"\U0001d120", u"\U0001d120") for b in range(256): if chr(b) not in '\n"\'\\abtnvfr01234567xuUN': check('\\' + chr(b), u'\\' + unichr(b))
def test_decode_errors(self): decode = codecs.unicode_escape_decode for c, d in ('x', 2), ('u', 4), ('U', 4): for i in range(d): self.assertRaises(UnicodeDecodeError, decode, "\\" + c + "0"*i) self.assertRaises(UnicodeDecodeError, decode, "[\\" + c + "0"*i + "]") data = "[\\" + c + "0"*i + "]\\" + c + "0"*i self.assertEqual(decode(data, "ignore"), (u"[]", len(data))) self.assertEqual(decode(data, "replace"), (u"[\ufffd]\ufffd", len(data))) self.assertRaises(UnicodeDecodeError, decode, r"\U00110000") self.assertEqual(decode(r"\U00110000", "ignore"), (u"", 10)) self.assertEqual(decode(r"\U00110000", "replace"), (u"\ufffd", 10))
def to_str(x): if isinstance(x, text_type): return x if isinstance(x, numbers.Number): x = str(x) elif x is None: x = '' return codecs.unicode_escape_decode(x)[0]
def test_unicode_escape(self): # Escape-decoding an unicode string is supported ang gives the same # result as decoding the equivalent ASCII bytes string. self.assertEqual(codecs.unicode_escape_decode(r"\u1234"), ("\u1234", 6)) self.assertEqual(codecs.unicode_escape_decode(br"\u1234"), ("\u1234", 6)) self.assertEqual(codecs.raw_unicode_escape_decode(r"\u1234"), ("\u1234", 6)) self.assertEqual(codecs.raw_unicode_escape_decode(br"\u1234"), ("\u1234", 6)) self.assertRaises(UnicodeDecodeError, codecs.unicode_escape_decode, br"\U00110000") self.assertEqual(codecs.unicode_escape_decode(r"\U00110000", "replace"), ("\ufffd", 10)) self.assertRaises(UnicodeDecodeError, codecs.raw_unicode_escape_decode, br"\U00110000") self.assertEqual(codecs.raw_unicode_escape_decode(r"\U00110000", "replace"), ("\ufffd", 10))