Re: [Tutor] python3 - °F/°C printing those degree signs

2018-08-08 Thread Steven D'Aprano
On Tue, Aug 07, 2018 at 08:43:53PM -0700, Evuraan wrote:
> >
> > You could try setting
> >
> > PYTHONIOENCODING="UTF-8"
> >
> > in your OS shell and see if that helps, but I suspect
> > there's a better way to deal with it...
> >
> 
> Thank you! That was it!

What system are you using that doesn't already have UTF-8 as the default 
encoding?


-- 
Steve
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] python3 - °F/°C printing those degree signs

2018-08-07 Thread Evuraan
>
> You could try setting
>
> PYTHONIOENCODING="UTF-8"
>
> in your OS shell and see if that helps, but I suspect
> there's a better way to deal with it...
>

Thank you! That was it!

Exporting thusly made it behave:

$ export PYTHONIOENCODING="UTF-8"
$ python3
Python 3.5.2 (default, Nov 23 2017, 16:37:01)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print('\u00b0'+ "F")
°F
>>>

I've come across this env variable even before, but overlooked it this
time again. Much appreciated!
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] python3 - °F/°C printing those degree signs

2018-08-07 Thread Steven D'Aprano
On Tue, Aug 07, 2018 at 02:32:58PM -0700, Evuraan wrote:
> Greetings!  How to print °F/°C etc in python3?


In Python 3, you should be able to do:

print('°F/°C')

directly. If you can't, your configuration is broken.

If you are including this is a .py file, make sure your text editor is 
set to use UTF-8 as the encoding.


> (This works on a WSL):

WSL?


> ~$ python3
> Python 3.5.2 (default, Nov 17 2016, 17:05:23)
> [GCC 5.4.0 20160609] on linux
> Type "help", "copyright", "credits" or "license" for more information.
> >>> import platform
> >>> platform.release()
> '4.4.0-17134-Microsoft'

Microsoft Linux?


> >>> print('\u00b0'+ " F")
> ° F


You don't need to use escape codes for this, but if you do, try this:

print('\u00b0 F')

 
> Elsewhere, it no longer seem to work:
> 
>  $ python3
> Python 3.5.2 (default, Nov 23 2017, 16:37:01)
> [GCC 5.4.0 20160609] on linux
> Type "help", "copyright", "credits" or "license" for more information.
> >>> import platform
> >>> platform.release()
> '4.4.0-21-generic'

What is this? OS X (Macinintosh), Windows, Windows with cgwin, Linux, 
some other Unix?

What does os.name return?


> >>> print('\u00b0'+ " F")
> Traceback (most recent call last):
>   File "", line 1, in 
> UnicodeEncodeError: 'ascii' codec can't encode character '\xb0' in
> position 0: ordinal not in range(128)

Until now, I would have said that error is literally impossible in 
Python 3.5.

Unless you have made a copy-and-paste error, and aren't showing us the 
correct output, I can't imagine how you are getting that error. This is 
very weird.

Hmmm... thinking... what do these return?

sys.getdefaultencoding()

sys.stdout.encoding



-- 
Steve
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] python3 - °F/°C printing those degree signs

2018-08-07 Thread Alan Gauld via Tutor
On 07/08/18 22:32, Evuraan wrote:

 print('\u00b0'+ " F")
> ° F
> 
> Elsewhere, it no longer seem to work:
> 
>  $ python3
> Python 3.5.2 (default, Nov 23 2017, 16:37:01)
> [GCC 5.4.0 20160609] on linux
> Type "help", "copyright", "credits" or "license" for more information.
 import platform
 platform.release()
> '4.4.0-21-generic'
 print('\u00b0'+ " F")
> Traceback (most recent call last):
>   File "", line 1, in 
> UnicodeEncodeError: 'ascii' codec can't encode character '\xb0' in
> position 0: ordinal not in range(128)

To some degree it depends on what you are printing on,
it needs to support unicode. Not all terminals do.

Secondly it needs to be set to unicode for its character
encoding and it appears from the error condition that
yours is set to ascii...

You could try setting

PYTHONIOENCODING="UTF-8"

in your OS shell and see if that helps, but I suspect
there's a better way to deal with it...

PS. You can check the current setting with:

>>> import sys
>>> sys.stdout.encoding

If it says ascii (or anything other than a Unicode setting)
then that's almost certainly your problem.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] python3 - °F/°C printing those degree signs

2018-08-07 Thread Evuraan
Greetings!  How to print °F/°C etc in python3?


(This works on a WSL):

~$ python3
Python 3.5.2 (default, Nov 17 2016, 17:05:23)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import platform
>>> platform.release()
'4.4.0-17134-Microsoft'
>>> print('\u00b0'+ " F")
° F

Elsewhere, it no longer seem to work:

 $ python3
Python 3.5.2 (default, Nov 23 2017, 16:37:01)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import platform
>>> platform.release()
'4.4.0-21-generic'
>>> print('\u00b0'+ " F")
Traceback (most recent call last):
  File "", line 1, in 
UnicodeEncodeError: 'ascii' codec can't encode character '\xb0' in
position 0: ordinal not in range(128)


How can we print °F / °C etc. - that should work everywhere on python3
so I can use the same code?

Thanks in advance!
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor