sys.stdin.encoding

A

aine_canby

The following line in my code is failing because sys.stdin.encoding is
Null. This has only started happening since I started working with
Pydef in Eclipse SDK. Any ideas?

uni=unicode(word,sys.stdin.encoding)

Thanks,

Aine.
 
D

Duncan Booth

The following line in my code is failing because sys.stdin.encoding is
Null.

I'll guess you mean None rather than Null.
This has only started happening since I started working with
Pydef in Eclipse SDK. Any ideas?

uni=unicode(word,sys.stdin.encoding)
You could give it a fallback value:

uni = unicode(word, sys.stdin.encoding or sys.getdefaultencoding())

or even just:

uni = unicode(word, sys.stdin.encoding or 'ascii')

which should be the same in all reasonable universes (although I did get
bitten recently when someone had changed the default encoding in a system).
 
A

aine_canby

Duncan Booth skrev:
I'll guess you mean None rather than Null.

You could give it a fallback value:

uni = unicode(word, sys.stdin.encoding or sys.getdefaultencoding())

or even just:

uni = unicode(word, sys.stdin.encoding or 'ascii')

which should be the same in all reasonable universes (although I did get
bitten recently when someone had changed the default encoding in a system).


Thanks for your help. The problem now is that I cant enter the Swedish
characters åöä etc without getting the following error -

Enter word> Påe
Traceback (most recent call last):
File "C:\Documents and Settings\workspace\simple\src\main.py", line
25, in <module>
archive.Test()
File "C:\Documents and Settings\workspace\simple\src\verb.py", line
192, in Test
uni=unicode(word,sys.stdin.encoding or sys.getdefaultencoding())
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 1:
ordinal not in range(128)

The call to sys.getdefaultencoding() returns ascii. Since I can enter
the characters åöä on the command line in Pydef/Eclipse doesn't that
mean that the stdin is not ascii? What should I do?

Thanks again,

Aine.
 
?

=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=

The following line in my code is failing because sys.stdin.encoding is
Null. This has only started happening since I started working with
Pydef in Eclipse SDK. Any ideas?

uni=unicode(word,sys.stdin.encoding)

That's a problem with pydev, where the standard machinery to determine
the terminal's encoding fail.

I have no idea yet how to fix this.

Regards,
Martin
 
D

Duncan Booth

The call to sys.getdefaultencoding() returns ascii. Since I can enter
the characters åöä on the command line in Pydef/Eclipse doesn't that
mean that the stdin is not ascii? What should I do?
I think that depends on what sort of script you are writing.

If it is just something for personal use on a single machine, or if you
know that the same encoding is going to be used on all systems where you
have this problem, then you could hardwire the encoding to whatever it
should be (maybe 'latin1').

If it is to be used across a variety of systems with different encodings
then you'll have to figure out some way to find the correct encoding.
 
A

aine_canby

Duncan Booth skrev:
I think that depends on what sort of script you are writing.

If it is just something for personal use on a single machine, or if you
know that the same encoding is going to be used on all systems where you
have this problem, then you could hardwire the encoding to whatever it
should be (maybe 'latin1').

If it is to be used across a variety of systems with different encodings
then you'll have to figure out some way to find the correct encoding.

Yeah I'm only learning python so I think I can go with latin1.

Cheers,

Aine.
 
L

Leo Kislov

Duncan Booth skrev:



Thanks for your help. The problem now is that I cant enter the Swedish
characters åöä etc without getting the following error -

Enter word> Påe
Traceback (most recent call last):
File "C:\Documents and Settings\workspace\simple\src\main.py", line
25, in <module>
archive.Test()
File "C:\Documents and Settings\workspace\simple\src\verb.py", line
192, in Test
uni=unicode(word,sys.stdin.encoding or sys.getdefaultencoding())
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 1:
ordinal not in range(128)

The call to sys.getdefaultencoding() returns ascii. Since I can enter
the characters åöä on the command line in Pydef/Eclipse doesn't that
mean that the stdin is not ascii? What should I do?

The workaround in your case is:

in the beginning of your program:

import sys
if hasattr(sys.stdin, 'encoding'):
console_encoding = sys.stdin.encoding
else:
import locale
locale_name, console_encoding = locale.getdefaultlocale()

and later:

uni = unicode(word, console_encoding)

But don't think it's portable, if you use other IDE or OS, it may not
work. It would be better if PyDev implemented sys.stdin.encoding

-- Leo
 
L

Leo Kislov

Martin said:
That's a problem with pydev, where the standard machinery to determine
the terminal's encoding fail.

I have no idea yet how to fix this.

Environmental variable TERMENCODING ? Heck, maybe this will catch on
and will be used by other languages, libraries, terminals, etc. It's
not really Python only problem.

-- Leo
 
?

=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=

Leo said:
Environmental variable TERMENCODING ? Heck, maybe this will catch on
and will be used by other languages, libraries, terminals, etc. It's
not really Python only problem.

I also considered environment variables. This can likely be made
available only in 2.6, though.

Plus, in the Eclipse case, we can't know for sure that stdout
really goes to the terminal - as it is just a pipe file descriptor
(Java is not capable of managing a full pseudo-terminal).

Regards,
Martin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top