[Tutor] Can't find error :-(
Steven D'Aprano
steve at pearwood.info
Sun Aug 28 16:15:13 CEST 2011
Lisi wrote:
> For future reference, how would I set about changing the encoding for just one
> character or file? I don't really want to change the encoding I use system
> wide.
You can set the encoding for the entire source file with an encoding
line like:
# -*- coding: utf-8 -*-
This MUST be in the first one or two lines of the file to take effect:
Optional shebang line.
Encoding line.
The exact form of the encoding line is very flexible. See here for more
information:
http://docs.python.org/reference/lexical_analysis.html#encoding-declarations
The encoding line is only necessary if you want to include non-ASCII
characters in your source code, either as variable names or in literal
strings, e.g.:
µ = 2.5
name = u"Michael Groß"
(Warning: just because you insert an encoding line in the source file,
doesn't mean your editor will obey it. Emacs and vim probably will, but
for most other editors, you may need to manually set the encoding. E.g.
in the kwrite editor, go to menu Tools > Encoding and choose whichever
one you prefer.)
Without an encoding line, you would have to use:
name = u"Michael Gro\xdf"
or
name = u"Michael Gro\N{LATIN SMALL LETTER SHARP S}"
You might be able to get away without an encoding line if the non-ASCII
characters are only in comments.
--
Steven
More information about the Tutor
mailing list