Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
54019966
Commit
54019966
authored
Feb 13, 2001
by
Ka-Ping Yee
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update to properly explain that the default Unicode encoding is ASCII, &c.
parent
ea4f931c
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
40 additions
and
22 deletions
+40
-22
Doc/tut/tut.tex
Doc/tut/tut.tex
+40
-22
No files found.
Doc/tut/tut.tex
View file @
54019966
...
@@ -772,17 +772,17 @@ u'Hello World !'
...
@@ -772,17 +772,17 @@ u'Hello World !'
\end{verbatim}
\end{verbatim}
The escape sequence
\code
{
\e
u0020
}
indicates to insert the Unicode
The escape sequence
\code
{
\e
u0020
}
indicates to insert the Unicode
character with the
HEX ordinal
0x0020 (the space character) at the
character with the
ordinal value
0x0020 (the space character) at the
given position.
given position.
Other characters are interpreted by using their respective ordinal
Other characters are interpreted by using their respective ordinal
value
directly as Unicode ordinal. Due to the fact that the lower 256
value
s directly as Unicode ordinals. If you have literal strings
Unicode are the same as the standard Latin-1 encoding used in many
in the standard Latin-1 encoding that is used in many Western countries,
western countries, the process of entering Unicode is greatly
you will find it convenient that the lower 256 characters
simplified
.
of Unicode are the same as the 256 characters of Latin-1
.
For experts, there is also a raw mode just like for normal
For experts, there is also a raw mode just like
the one
for normal
strings. You have to pre
pend the string with a small '
r' to have
strings. You have to pre
fix the opening quote with 'u
r' to have
Python use the
\emph
{
Raw-Unicode-Escape
}
encoding. It will only apply
Python use the
\emph
{
Raw-Unicode-Escape
}
encoding. It will only apply
the above
\code
{
\e
uXXXX
}
conversion if there is an uneven number of
the above
\code
{
\e
uXXXX
}
conversion if there is an uneven number of
backslashes in front of the small 'u'.
backslashes in front of the small 'u'.
...
@@ -801,32 +801,50 @@ Apart from these standard encodings, Python provides a whole set of
...
@@ -801,32 +801,50 @@ Apart from these standard encodings, Python provides a whole set of
other ways of creating Unicode strings on the basis of a known
other ways of creating Unicode strings on the basis of a known
encoding.
encoding.
The built-in function
\function
{
unicode()
}
\bifuncindex
{
unicode
}
provides access
The built-in function
\function
{
unicode()
}
\bifuncindex
{
unicode
}
provides
to all registered Unicode codecs (COders and DECoders). Some of the
access to all registered Unicode codecs (COders and DECoders). Some of
more well known encodings which these codecs can convert are
the more well known encodings which these codecs can convert are
\emph
{
Latin-1
}
,
\emph
{
ASCII
}
,
\emph
{
UTF-8
}
and
\emph
{
UTF-16
}
. The latter two
\emph
{
Latin-1
}
,
\emph
{
ASCII
}
,
\emph
{
UTF-8
}
, and
\emph
{
UTF-16
}
.
are variable-length encodings which store Unicode characters
The latter two are variable-length encodings that store each Unicode
in blocks of 8 or 16 bits. To print a Unicode string or write it to a file,
character in one or more bytes. The default encoding is
you must convert it to a string with the
\method
{
encode()
}
method.
normally set to ASCII, which passes through characters in the range
0 to 127 and rejects any other characters with an error.
When a Unicode string is printed, written to a file, or converted
with
\function
{
str()
}
, conversion takes place using this default encoding.
\begin{verbatim}
>>> u"abc"
u'abc'
>>> str(u"abc")
'abc'
>>> u"äöü"
u'
\xe
4
\xf
6
\xfc
'
>>> str(u"äöü")
Traceback (most recent call last):
File "<stdin>", line 1, in ?
UnicodeError: ASCII encoding error: ordinal not in range(128)
\end{verbatim}
To convert a Unicode string into an 8-bit string using a specific
encoding, Unicode objects provide an
\function
{
encode()
}
method
that takes one argument, the name of the encoding. Lowercase names
for encodings are preferred.
\begin{verbatim}
\begin{verbatim}
>>> u"äöü"
>>> u"äöü".encode('utf-8')
u'
\3
44
\3
66
\3
74'
'
\xc
3
\xa
4
\xc
3
\xb
6
\xc
3
\xbc
'
>>> u"äöü".encode('UTF-8')
'
\3
03
\2
44
\3
03
\2
66
\3
03
\2
74'
\end{verbatim}
\end{verbatim}
If you have data in a specific encoding and want to produce a
If you have data in a specific encoding and want to produce a
corresponding Unicode string from it, you can use the
corresponding Unicode string from it, you can use the
\function
{
unicode()
}
function with the encoding name as second
\function
{
unicode()
}
function with the encoding name as
the
second
argument.
argument.
\begin{verbatim}
\begin{verbatim}
>>> unicode('
\
3
03
\2
44
\3
03
\2
66
\3
03
\2
74','UTF
-8')
>>> unicode('
\
xc
3
\xa
4
\xc
3
\xb
6
\xc
3
\xbc
', 'utf
-8')
u'
\
3
44
\3
66
\3
74
'
u'
\
xe
4
\xf
6
\xfc
'
\end{verbatim}
\end{verbatim}
\subsection
{
Lists
\label
{
lists
}}
\subsection
{
Lists
\label
{
lists
}}
Python knows a number of
\emph
{
compound
}
data types, used to group
Python knows a number of
\emph
{
compound
}
data types, used to group
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment