Python - standard library - ascii () function

I started looking at the standard Python library: ( http://docs.python.org/3/library/functions.html )

In an attempt to continue exploring basic python. When it comes to explaining the ascii () function, I don't find it clear.

Can someone provide a brief explanation giving examples of useful situations in which you can use the ascii () function, please?

+4
source share
2 answers

ascii()is a function that encodes output repr()to use escape sequences for any code point in the output created repr()that is not in the ASCII range.

So, Latin code 1, such as ë, is represented in its place by the Python escape sequence \xeb.

This is the standard view in Python 2; Python 3 repr()leaves most Unicode codes as their actual output if it is a printable character:

>>> print(repr('ë'))
'ë'
>>> print(ascii('ë'))
'\xeb'

Both outputs are valid Python string literals, but the latter only uses ASCII characters, while the former requires Unicode-compatible encoding.

Unicode U + 0100 U + FFFF \uxxxx escape- , - \Uxxxxxxxx. . escape- Python.

repr(), ascii() - , . repr(), ascii() Unchode .

- , ; ë : U + 00EB ASCII e diaeresis ¨ ( U + 0308):

>>> import unicodedata
>>> one, two = 'ë', unicodedata.normalize('NFD', 'ë')
>>> print(one, two)
ë ë
>>> print(repr(one), repr(two))
'ë' 'ë'
>>> print(ascii(one), ascii(two))
'\xeb' 'e\u0308'

ascii() , two .

+4

ascii() , . , mojibake - , ascii , , .

Stackoverflow , , , mojibake, . ascii ( repr Python 2), .

+1

Source: https://habr.com/ru/post/1526339/


All Articles