Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #76297 > unrolled thread
| Started by | marc.vanhoomissen@gmail.com |
|---|---|
| First post | 2014-08-14 05:35 -0700 |
| Last post | 2014-08-15 01:23 -0700 |
| Articles | 14 — 9 participants |
Back to article view | Back to comp.lang.python
Unable to run print('Réussi') on windows and on linux marc.vanhoomissen@gmail.com - 2014-08-14 05:35 -0700
Re: Unable to run print('Réussi') on windows and on linux YBM <ybmess@nooos.fr.invalid> - 2014-08-14 15:31 +0200
Re: Unable to run print('Réussi') on windows and on linux marc.vanhoomissen@gmail.com - 2014-08-14 07:04 -0700
Re: Unable to run print('Réussi') on windows and on linux Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-08-14 15:38 +0100
Re: Unable to run print('Réussi') on windows and on linux YBM <ybmess@nooos.fr.invalid> - 2014-08-14 16:47 +0200
Re: Unable to run print('Réussi') on windows and on linux Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-08-15 01:21 +1000
Re: Unable to run print('Réussi') on windows and on linux Ian Kelly <ian.g.kelly@gmail.com> - 2014-08-14 10:12 -0600
Re: Unable to run print('Réussi') on windows and on linux wxjmfauth@gmail.com - 2014-08-15 01:25 -0700
Re: Unable to run print('Réussi') on windows and on linux Vincent Vande Vyvre <vincent.vande.vyvre@telenet.be> - 2014-08-14 16:46 +0200
Re: Unable to run print('Réussi') on windows and on linux Vincent Vande Vyvre <vincent.vande.vyvre@telenet.be> - 2014-08-14 15:22 +0200
Re: Unable to run print('Réussi') on windows and on linux marc.vanhoomissen@gmail.com - 2014-08-14 07:01 -0700
Re: Unable to run print('Réussi') on windows and on linux Peter Otten <__peter__@web.de> - 2014-08-14 18:00 +0200
Re: Unable to run print('Réussi') on windows and on linux Anssi Saari <as@sci.fi> - 2014-08-14 17:36 +0300
Re: Unable to run print('Réussi') on windows and on linux wxjmfauth@gmail.com - 2014-08-15 01:23 -0700
| From | marc.vanhoomissen@gmail.com |
|---|---|
| Date | 2014-08-14 05:35 -0700 |
| Subject | Unable to run print('Réussi') on windows and on linux |
| Message-ID | <4dd67a80-b466-4866-ab67-d9a94c0b7ddf@googlegroups.com> |
Hello,
This very simple program runs well on windows 7
# -*- utf8 -*-
print('Réussi')
But, when I start the vrey same file on Linux (ubuntu 14), I got:
Traceback (most recent call last):
File "/partages/bureau/PB/Dev/Python3/test.py", line 2, in <module>
print('R\xe9ussi')
UnicodeEncodeError: 'ascii' codec can't encode character '\xe9' in position 1: ordinal not in range(128)
What should i do to let the same program run on both OS, without changes?
Thank you for your answer
Marc Vanhoomissen
[toc] | [next] | [standalone]
| From | YBM <ybmess@nooos.fr.invalid> |
|---|---|
| Date | 2014-08-14 15:31 +0200 |
| Message-ID | <53ecba3e$0$1989$426a74cc@news.free.fr> |
| In reply to | #76297 |
Le 14/08/2014 14:35, marc.vanhoomissen@gmail.com a écrit :
> Hello,
>
> This very simple program runs well on windows 7
>
> # -*- utf8 -*-
> print('Réussi')
>
> But, when I start the vrey same file on Linux (ubuntu 14), I got:
>
> Traceback (most recent call last):
> File "/partages/bureau/PB/Dev/Python3/test.py", line 2, in <module>
> print('R\xe9ussi')
> UnicodeEncodeError: 'ascii' codec can't encode character '\xe9' in position 1: ordinal not in range(128)
>
> What should i do to let the same program run on both OS, without changes?
the correct comment line should be : # -*- encoding: utf-8 -*-
and it could be usefull to begin with #!/usr/bin/env python
or #!/usr/bin/env python3 :
$ cat > réussi.py
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
print('Réussi');
^D
$ chmod +x réussi.py
$ ./réussi.py
Réussi
$ python réussi.py
Réussi
[toc] | [prev] | [next] | [standalone]
| From | marc.vanhoomissen@gmail.com |
|---|---|
| Date | 2014-08-14 07:04 -0700 |
| Message-ID | <d460fa66-e881-4b80-9f86-cf82b3aa45ca@googlegroups.com> |
| In reply to | #76302 |
Hello YBM, I tried your suggestions, without improvement. Further, see my answer to Vincent Vande Vyre Thanks anyway.
[toc] | [prev] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2014-08-14 15:38 +0100 |
| Message-ID | <mailman.12998.1408027119.18130.python-list@python.org> |
| In reply to | #76306 |
On 14/08/2014 15:04, marc.vanhoomissen@gmail.com wrote: > Hello YBM, > I tried your suggestions, without improvement. > Further, see my answer to Vincent Vande Vyre > Thanks anyway. > I'm pleased to see that you have answers. In return would you please quote the context. Could you also access this list via https://mail.python.org/mailman/listinfo/python-list or read and action this https://wiki.python.org/moin/GoogleGroupsPython to prevent us seeing double line spacing and single line paragraphs, thanks. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence
[toc] | [prev] | [next] | [standalone]
| From | YBM <ybmess@nooos.fr.invalid> |
|---|---|
| Date | 2014-08-14 16:47 +0200 |
| Message-ID | <53eccc08$0$2932$426a34cc@news.free.fr> |
| In reply to | #76306 |
Le 14/08/2014 16:04, marc.vanhoomissen@gmail.com a écrit : > Hello YBM, > I tried your suggestions, without improvement. > Further, see my answer to Vincent Vande Vyre > Thanks anyway. This is indeed very surprising. Are you sure that you have *exactly* this line at the first or second (not later !) line of your script : # -*- encoding: utf-8 -*- if a single caracter differs, it would fail.
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2014-08-15 01:21 +1000 |
| Message-ID | <53ecd406$0$29971$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #76312 |
YBM wrote: > Le 14/08/2014 16:04, marc.vanhoomissen@gmail.com a écrit : >> Hello YBM, >> I tried your suggestions, without improvement. >> Further, see my answer to Vincent Vande Vyre >> Thanks anyway. > > This is indeed very surprising. Are you sure that you > have *exactly* this line at the first or second (not > later !) line of your script : > > # -*- encoding: utf-8 -*- > > if a single caracter differs, it would fail. That's not correct. The encoding declaration is very flexible. Any of these will be accepted: # This file uses the encoding: utf_8 # coding=UTF-8 # -*- coding: utf8 -*- # vim: set fileencoding=utf-8 : # Uses encoding:utf8 # I want my encoding=UtF_8 okay! #### textencoding= UTf-8 blah blah blah and many, many other varieties. The rules are: (1) It must be a comment; (2) It must be in the first or second line of the file; (3) It must match the regular expression r"coding[:=]\s*([-\w.]+)" However, just because you declare the file to be UTF-8, doesn't mean it *actually is* UTF-8. If your text editor is configured to use (say) Latin-1, a UTF-8 encoding declaration will just give you garbage. * Fix your system to use UTF-8 by default. * Fix your editor to use UTF-8. * Add a UTF-8 encoding declaration. And then things should work. -- Steven
[toc] | [prev] | [next] | [standalone]
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2014-08-14 10:12 -0600 |
| Message-ID | <mailman.13003.1408032785.18130.python-list@python.org> |
| In reply to | #76314 |
On Thu, Aug 14, 2014 at 9:21 AM, Steven D'Aprano <steve+comp.lang.python@pearwood.info> wrote: > YBM wrote: > >> Le 14/08/2014 16:04, marc.vanhoomissen@gmail.com a écrit : >>> Hello YBM, >>> I tried your suggestions, without improvement. >>> Further, see my answer to Vincent Vande Vyre >>> Thanks anyway. >> >> This is indeed very surprising. Are you sure that you >> have *exactly* this line at the first or second (not >> later !) line of your script : >> >> # -*- encoding: utf-8 -*- >> >> if a single caracter differs, it would fail. > > That's not correct. The encoding declaration is very flexible. Any of these > will be accepted: > > # This file uses the encoding: utf_8 > # coding=UTF-8 > # -*- coding: utf8 -*- > # vim: set fileencoding=utf-8 : > # Uses encoding:utf8 > # I want my encoding=UtF_8 okay! > #### textencoding= UTf-8 blah blah blah > > and many, many other varieties. The rules are: > > (1) It must be a comment; > > (2) It must be in the first or second line of the file; > > (3) It must match the regular expression r"coding[:=]\s*([-\w.]+)" > > > However, just because you declare the file to be UTF-8, doesn't mean it > *actually is* UTF-8. If your text editor is configured to use (say) > Latin-1, a UTF-8 encoding declaration will just give you garbage. > > * Fix your system to use UTF-8 by default. > > * Fix your editor to use UTF-8. > > * Add a UTF-8 encoding declaration. > > And then things should work. And apart from all of that, if the OP is really using Python 3 then UTF-8 is the default source encoding anyway.
[toc] | [prev] | [next] | [standalone]
| From | wxjmfauth@gmail.com |
|---|---|
| Date | 2014-08-15 01:25 -0700 |
| Message-ID | <c5b27af5-ce27-4cbd-b4bd-01f50b8d129f@googlegroups.com> |
| In reply to | #76314 |
Le jeudi 14 août 2014 17:21:41 UTC+2, Steven D'Aprano a écrit :
>
> * Fix your system to use UTF-8 by default.
>
>
%%%%%
Sorry, but no. This is not a correct way of working.
>>> u = 'abc需αб'
>>> u; print(u)
'abc需αб'
abc需αб
>>> sys.stdout.encoding = 'cp437'
>>> print(u)
Traceback (most recent call last):
File "<eta last command>", line 1, in <module>
File "D:\jm\jmpy\eta\eta41beta1\etastdio.py", line 193, in write
s = s.encode(self.pencoding).decode('cp1252')
File "c:\python32\lib\encodings\cp437.py", line 12, in encode
return codecs.charmap_encode(input,errors,encoding_map)
UnicodeEncodeError: 'charmap' codec can't encode characters in position 4-5:
character maps to <undefined>
>>> print(u.encode(sys.stdout.encoding, 'replace'))
abcé??α?
>>> sys.stdout.encoding = 'utf-16-le'
>>> print(u)
a b c é S¬ ±1
>>> print(u.encode(sys.stdout.encoding, 'replace'))
abc需αб
>>> # etc
jmf
[toc] | [prev] | [next] | [standalone]
| From | Vincent Vande Vyvre <vincent.vande.vyvre@telenet.be> |
|---|---|
| Date | 2014-08-14 16:46 +0200 |
| Message-ID | <mailman.13000.1408027618.18130.python-list@python.org> |
| In reply to | #76302 |
Le 14/08/2014 15:31, YBM a écrit :
> Le 14/08/2014 14:35, marc.vanhoomissen@gmail.com a écrit :
>> Hello,
>>
>> This very simple program runs well on windows 7
>>
>> # -*- utf8 -*-
>> print('Réussi')
>>
>> But, when I start the vrey same file on Linux (ubuntu 14), I got:
>>
>> Traceback (most recent call last):
>> File "/partages/bureau/PB/Dev/Python3/test.py", line 2, in <module>
>> print('R\xe9ussi')
>> UnicodeEncodeError: 'ascii' codec can't encode character '\xe9' in
>> position 1: ordinal not in range(128)
>>
>> What should i do to let the same program run on both OS, without
>> changes?
>
> the correct comment line should be : # -*- encoding: utf-8 -*-
> and it could be usefull to begin with #!/usr/bin/env python
> or #!/usr/bin/env python3 :
>
> $ cat > réussi.py
> #!/usr/bin/env python
> # -*- encoding: utf-8 -*-
> print('Réussi');
> ^D
> $ chmod +x réussi.py
> $ ./réussi.py
> Réussi
> $ python réussi.py
> Réussi
>
>
Nothing to do with the file encoding.
... if the OP use really Python 3
[toc] | [prev] | [next] | [standalone]
| From | Vincent Vande Vyvre <vincent.vande.vyvre@telenet.be> |
|---|---|
| Date | 2014-08-14 15:22 +0200 |
| Message-ID | <mailman.12996.1408023268.18130.python-list@python.org> |
| In reply to | #76297 |
Le 14/08/2014 14:35, marc.vanhoomissen@gmail.com a écrit :
> Hello,
>
> This very simple program runs well on windows 7
>
> # -*- utf8 -*-
> print('Réussi')
>
> But, when I start the vrey same file on Linux (ubuntu 14), I got:
>
> Traceback (most recent call last):
> File "/partages/bureau/PB/Dev/Python3/test.py", line 2, in <module>
> print('R\xe9ussi')
> UnicodeEncodeError: 'ascii' codec can't encode character '\xe9' in position 1: ordinal not in range(128)
>
> What should i do to let the same program run on both OS, without changes?
>
> Thank you for your answer
>
> Marc Vanhoomissen
No problem on Ubuntu
Python 3.2.3 (default, Feb 27 2014, 21:33:50)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print("Réussi")
Réussi
Are you really using Python 3 ?
$ python3 test.py
[toc] | [prev] | [next] | [standalone]
| From | marc.vanhoomissen@gmail.com |
|---|---|
| Date | 2014-08-14 07:01 -0700 |
| Message-ID | <41563121-149d-401c-94ed-9c5c74e55ba3@googlegroups.com> |
| In reply to | #76303 |
Le jeudi 14 août 2014 15:22:52 UTC+2, Vincent Vande Vyvre a écrit :
> Le 14/08/2014 14:35, marc.vanhoomissen@gmail.com a �crit :
>
> > Hello,
>
> >
>
> > This very simple program runs well on windows 7
>
> >
>
> > # -*- utf8 -*-
>
> > print('R�ussi')
>
> >
>
> > But, when I start the vrey same file on Linux (ubuntu 14), I got:
>
> >
>
> > Traceback (most recent call last):
>
> > File "/partages/bureau/PB/Dev/Python3/test.py", line 2, in <module>
>
> > print('R\xe9ussi')
>
> > UnicodeEncodeError: 'ascii' codec can't encode character '\xe9' in position 1: ordinal not in range(128)
>
> >
>
> > What should i do to let the same program run on both OS, without changes?
>
> >
>
> > Thank you for your answer
>
> >
>
> > Marc Vanhoomissen
>
> No problem on Ubuntu
>
>
>
> Python 3.2.3 (default, Feb 27 2014, 21:33:50)
>
> [GCC 4.6.3] on linux2
>
> Type "help", "copyright", "credits" or "license" for more information.
>
> >>> print("R�ussi")
>
> R�ussi
>
>
>
> Are you really using Python 3 ?
>
>
>
> $ python3 test.py
Actually, when I try using a terminal, it works:
$ python3 test.py
Réussi
But when I issue the same command using webmin (v. 1.700 - shell command), I got:
> python3 test.py
Traceback (most recent call last):
File "test.py", line 2, in <module>
print('R\xe9ussi')
UnicodeEncodeError: 'ascii' codec can't encode character '\xe9' in position 1: ordinal not in range(128)
So, I guess it is merely a problem of webmin.
[toc] | [prev] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2014-08-14 18:00 +0200 |
| Message-ID | <mailman.13001.1408032080.18130.python-list@python.org> |
| In reply to | #76305 |
marc.vanhoomissen@gmail.com wrote:
> Le jeudi 14 août 2014 15:22:52 UTC+2, Vincent Vande Vyvre a écrit :
>> Are you really using Python 3 ?
>> $ python3 test.py
>
> Actually, when I try using a terminal, it works:
> $ python3 test.py
> Réussi
>
> But when I issue the same command using webmin (v. 1.700 - shell command),
> I got:
>> python3 test.py
> Traceback (most recent call last):
> File "test.py", line 2, in <module>
> print('R\xe9ussi')
> UnicodeEncodeError: 'ascii' codec can't encode character '\xe9' in
> position 1: ordinal not in range(128)
>
> So, I guess it is merely a problem of webmin.
I have no idea how that might interact with webmin, but you could try to set
the environment variable
PYTHONIOENCODING=utf-8
[toc] | [prev] | [next] | [standalone]
| From | Anssi Saari <as@sci.fi> |
|---|---|
| Date | 2014-08-14 17:36 +0300 |
| Message-ID | <vg3sikz9lza.fsf@coffee.modeemi.fi> |
| In reply to | #76297 |
marc.vanhoomissen@gmail.com writes: > What should i do to let the same program run on both OS, without changes? You'd want to set the locale on your Ubuntu box to a UTF8 locale. On the command line you'd run sudo dpkg-reconfigure locales and proceed from there, but I guess there might be gooey way to do that too. But really, it's a Linux configuration question, not a Python question.
[toc] | [prev] | [next] | [standalone]
| From | wxjmfauth@gmail.com |
|---|---|
| Date | 2014-08-15 01:23 -0700 |
| Message-ID | <ef036ddf-dddd-4ff9-93f4-df753240a5a3@googlegroups.com> |
| In reply to | #76297 |
Le jeudi 14 août 2014 14:35:51 UTC+2, marc.van...@gmail.com a écrit :
> Hello,
>
>
>
> This very simple program runs well on windows 7
>
>
>
> # -*- utf8 -*-
>
> print('Réussi')
>
>
>
> But, when I start the vrey same file on Linux (ubuntu 14), I got:
>
>
>
> Traceback (most recent call last):
>
> File "/partages/bureau/PB/Dev/Python3/test.py", line 2, in <module>
>
> print('R\xe9ussi')
>
> UnicodeEncodeError: 'ascii' codec can't encode character '\xe9' in position 1: ordinal not in range(128)
>
>
>
> What should i do to let the same program run on both OS, without changes?
>
>
>
> Thank you for your answer
>
My guess.
Your created "utf-8" file may contain a "mark", wrongly called BOM,
and your *x toll is not handling it.
jmf
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web