Diferencia entre revisiones de «Usuario:Lmorillas/intropyaytozgz/unicode»
De WikiEducator
Línea 34: | Línea 34: | ||
>>> len(aragon_uni) | >>> len(aragon_uni) | ||
6 | 6 | ||
+ | </source> | ||
}} | }} | ||
Línea 42: | Línea 43: | ||
s.decode(encoding) | s.decode(encoding) | ||
# <type 'str'> to <type 'unicode'> | # <type 'str'> to <type 'unicode'> | ||
+ | >>> 'caña'.decode() | ||
+ | Traceback (most recent call last): | ||
+ | File "<stdin>", line 1, in <module> | ||
+ | UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 2: ordinal not in range(128) | ||
+ | >>> 'caña'.decode(errors='ignore') | ||
+ | u'caa' | ||
+ | >>> 'caña'.decode(errors='replace') | ||
+ | u'ca\ufffd\ufffda' | ||
+ | >>> 'caña'.decode('utf-8') | ||
+ | u'ca\xf1a' | ||
u.encode(encoding) | u.encode(encoding) | ||
# <type 'unicode'> to <type 'str'> | # <type 'unicode'> to <type 'str'> | ||
+ | >>> u'caña'.encode('ascii', 'ignore') | ||
+ | 'caa' | ||
+ | >>> u'caña'.encode('ascii', 'replace') | ||
+ | 'ca?a' | ||
+ | >>> u'caña'.encode('ascii') | ||
+ | Traceback (most recent call last): | ||
+ | File "<stdin>", line 1, in <module> | ||
+ | UnicodeEncodeError: 'ascii' codec can't encode character u'\xf1' in position 2: ordinal not in range(128) | ||
+ | >>> u'caña'.encode('utf-8') | ||
+ | 'ca\xc3\xb1a' | ||
</source> | </source> | ||
Línea 63: | Línea 84: | ||
</source> | </source> | ||
}} | }} | ||
+ | |||
+ | open(filename, mode='rb', encoding=None, errors='strict', buffering=1) |
Revisión de 08:48 3 dic 2012
Una cadena Unicode es una secuencia de code points, que son números de 0 a 0x10ffff. Esta secuencia se tiene que representar en un conjunto de bytes (valores de 0 a 255) en memoria. Las reglas para traducir una cadena Unicode a una secuencia de bytes se llama encoding.
|
¿Qué significa esto? UnicodeDecodeError: 'ascii' codec can't decode byte 0xc4 in position 10: ordinal not in range(128) >>> f = open('aragon.txt') >>> aragon_utf8 = f.read() >>> aragon_utf8 'Arag\xc3\xb3n' >>> len(aragon_utf8) 7 >>>type(aragon_utf8) <type 'str'> >>> unicode(aragon_utf8) UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 4: ordinal not in range(128) # el problema: >>> sys.getdefaultencoding() 'ascii' >>> aragon_uni = aragon_utf8.decode('utf-8') >>> aragon_uni u'Arag\xf3n' >>> len(aragon_uni) 6 |
{{Objetivo|Title=Conversión y codificación|
s.decode(encoding) # <type 'str'> to <type 'unicode'> >>> 'caña'.decode() Traceback (most recent call last): File "<stdin>", line 1, in <module> UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 2: ordinal not in range(128) >>> 'caña'.decode(errors='ignore') u'caa' >>> 'caña'.decode(errors='replace') u'ca\ufffd\ufffda' >>> 'caña'.decode('utf-8') u'ca\xf1a' u.encode(encoding) # <type 'unicode'> to <type 'str'> >>> u'caña'.encode('ascii', 'ignore') 'caa' >>> u'caña'.encode('ascii', 'replace') 'ca?a' >>> u'caña'.encode('ascii') Traceback (most recent call last): File "<stdin>", line 1, in <module> UnicodeEncodeError: 'ascii' codec can't encode character u'\xf1' in position 2: ordinal not in range(128) >>> u'caña'.encode('utf-8') 'ca\xc3\xb1a'
import unicodedata def textoplano(s): def normalize(c): return unicodedata.normalize("NFD", c)[0] # NFD: Normal Form Decomposition return ''.join(normalize(c) for c in s) >>> textoplano('una cigüeña hábil') "una ciguena habil" |
open(filename, mode='rb', encoding=None, errors='strict', buffering=1)