Re: encoding="UTF-8"?

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: encoding="UTF-8"?

WingIDE - User mailing list
Urmas Punane wrote:
> J├ż├ż├ż├żr hakkas liikuma, h├żrrased vannutatud mehed.
>
> P├Ąrsast kotis p├Ąllumeestele
>
> p├╝hadeks p├╝ramiid ├Ąunaaeda
>
> Maybe I just don’tknow how to change the text encoding in Wing IDE..

There are at least two issues here:  One is that you need to specify the
encoding in your source file like so on the first or second line:

# coding: utf-8

The other issue (maybe) is the I/O encoding, which can actually differ
from the encoding of the source code but hopefully is also utf-8.  You
can set the encoding preferences on the Debugging > I/O page to utf-8
and it may fix it.

However, the default I/O encoding Python uses when printing can be set
with the environment variable PYTHONIOENCODING so that may be setting
something other than utf-8.  Also, if that is not set Python tries to
guess the most appropriate output encoding for your system.  I think the
default encoding Python is going to use is given by
sys.getdefaultencoding() or you can check sys.stdout.encoding.  On
Windows I suspect it is in fact not utf-8.

Keep in mind that when you have a string like "pühadeks püramiid
õunaaeda" in Python 3 it is a unicode string that is created from the
encoded text of your source file using the encoding you specified in
your # coding comment (or the default encoding Python determines for
your system if there is no coding comment).  To be explicit about what
encoding gets printed you can do something like this to convert the
unicode string into a particular text encoding:

mystring = "pühadeks püramiid õunaaeda"
print(mystring.encode('utf-8'))

At this point I would recommend just using utf-8 for everything, as much
as possible.  So you may just want to define a custom function that
encodes to utf-8 and prints, since it's good to be explicit and not
assume PYTHONIOENCODING is defined, and also have "# coding: utf-8" at
the top of all your source files.

Please let me know if this isn't clear.

Thanks,

--

Stephan Deibel
Wingware | Python IDE

The Intelligent Development Environment for Python Programmers

wingware.com

_________________________________________________
Wing IDE users list
http://wingware.com/lists/wingide
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: encoding="UTF-8"?

WingIDE - User mailing list
Urmas Punane wrote:

> I tried it too:
>
> mystring = "pühadeks püramiid õunaaeda"
> print(mystring.encode('utf-8'))
>
> and I got such a error:
> --
> File "E:\Hello-world.py", line 2, in<module>
>    print(mystring.encode('utf-8'))
>
> UnicodeDecodeError: 'ascii' codec can't decode byte 0xfc in position 1: ordinal not in range(128)

Sorry, I assumed you were using Python 3 because you had "print(x)" and
not "print x".  The above is for Python 3.

In Python 2 mystring would not be a unicode string but instead an
encoded string in the encoding of the source file.  In that case
printing utf-8 (assuming you have '#coding: utf-8' at the top of your
source file) would be just "print mystring".

In Python 2, if you want a unicode string you would do:

umystring = unicode(mystring, 'utf-8')

In general I'd recommend using Python 3 because the string encoding
picture is generally simpler there.  Since all strings are unicode in
Python 3 (unless you explicitly create a bytes object), it's only during
input and output that you worry about converting into a particular
encoding.  In Python 2, you basically end up doing manual work to make
to ensure you have unicode objects or strings with a known encoding and
it's easier to make mistakes.

Please let me know if this does not help.

Thanks,

--

Stephan Deibel
Wingware | Python IDE

The Intelligent Development Environment for Python Programmers

wingware.com

_________________________________________________
Wing IDE users list
http://wingware.com/lists/wingide
Loading...