Unicode () UTF-8, , . 7- ASCII , .
, , UTF-8 . , .
©, ® ™, 2, 2 3 UTF-8. UTF-8.
utfbytes = "\xc2\xa9 \xc2\xae \xe2\x84\xa2"
print utfbytes, len(utfbytes)
for b in utfbytes:
print b, repr(b)
uni = utfbytes.decode('utf-8')
print uni, len(uni)
© ® ™ 9
'\xc2'
'\xa9'
' '
'\xc2'
'\xae'
' '
'\xe2'
'\x84'
'\xa2'
© ® ™ 5
Stack Overflow, , Unicode: , ( !)
Unicode HOWTO Python Ned Batchelder Pragmatic Unicode, "Unipain".
UTF-8. , , , .
utfbytes = "\xc2\xa9 \xc2\xae \xe2\x84\xa2"
widths = (2, 1, 2, 1, 3)
start = 0
for w in widths:
print "%d %d [%s]" % (start, w, utfbytes[start:start+w])
start += w
0 2 [©]
2 1 [ ]
3 2 [®]
5 1 [ ]
6 3 [™]
FWIW, Python 3 :
utfbytes = b"\xc2\xa9 \xc2\xae \xe2\x84\xa2"
widths = (2, 1, 2, 1, 3)
start = 0
for w in widths:
s = utfbytes[start:start+w]
print("%d %d [%s]" % (start, w, s.decode()))
start += w
UTF-8, . UTF-8 , UTF-8.
Python 2 , ; , .
def get_width(b):
if b <= '\x7f':
return 1
elif '\x80' <= b <= '\xbf':
raise ValueError('Bad alignment: %r is a continuation byte' % b)
elif '\xc0' <= b <= '\xdf':
return 2
elif '\xe0' <= b <= '\xef':
return 3
elif '\xf0' <= b <= '\xf7':
return 4
else:
raise ValueError('%r is not a single byte' % b)
utfbytes = b"\xc2\xa9 \xc2\xae \xe2\x84\xa2"
start = 0
while start < len(utfbytes):
b = utfbytes[start]
w = get_width(b)
s = utfbytes[start:start+w]
print "%d %d [%s]" % (start, w, s)
start += w
, : .
, Python 3 get_width , UTF-8.
def get_width(b):
if b <= 0x7f:
return 1
elif 0x80 <= b <= 0xbf:
raise ValueError('Bad alignment: %r is a continuation byte' % b)
elif 0xc0 <= b <= 0xdf:
return 2
elif 0xe0 <= b <= 0xef:
return 3
elif 0xf0 <= b <= 0xf7:
return 4
else:
raise ValueError('%r is not a single byte' % b)
def decode_utf8(utfbytes):
start = 0
uni = []
while start < len(utfbytes):
b = utfbytes[start]
w = get_width(b)
if w == 1:
n = b
else:
n = b & (0x7f >> w)
for b in utfbytes[start+1:start+w]:
if not 0x80 <= b <= 0xbf:
raise ValueError('Not a continuation byte: %r' % b)
n <<= 6
n |= b & 0x3f
uni.append(chr(n))
start += w
return ''.join(uni)
utfbytes = b'\xc2\xa9 \xc2\xae \xe2\x84\xa2'
print(utfbytes.decode('utf8'))
print(decode_utf8(utfbytes))
© ® ™
© ® ™