N
Neil Cerutti
How do I cope with faulty encoding settings?
I'm writing an application that needs all internal character data
to be stored in iso-8859-1. It also must allow input and output
using stdin and stdout.
This works just fine with the Windows binary of Python.
sys.stdin.encoding is correctly set to the encoding of the
current terminal ('cp437').
s = sys.stdin.readline()
# Convert to iso-8859-1.
s = s.decode(sys.stdin.encoding).encode('iso-8859-1')
Granted, users are constrained to entering characters in the
cp437 charset, but that's better than the following.
The Cygwin binary I have (2.4.3) reports sys.stdin.encoding as
'US-ASCII', which is quite wrong. A Cygwin terminal uses, as far
as I can tell, iso-8859-1. This renders the above construction
useless if the user enters any character codes above 128.
Using raw_input instead of readline addresses the problem by making
it impossible to enter non-ascii text.
Please advise.
This is only a temporary problem, as eventually this application
will use Tkinter as an interface instead. But of course then I'll
probably have a bunch of new problems.
I'm writing an application that needs all internal character data
to be stored in iso-8859-1. It also must allow input and output
using stdin and stdout.
This works just fine with the Windows binary of Python.
sys.stdin.encoding is correctly set to the encoding of the
current terminal ('cp437').
s = sys.stdin.readline()
# Convert to iso-8859-1.
s = s.decode(sys.stdin.encoding).encode('iso-8859-1')
Granted, users are constrained to entering characters in the
cp437 charset, but that's better than the following.
The Cygwin binary I have (2.4.3) reports sys.stdin.encoding as
'US-ASCII', which is quite wrong. A Cygwin terminal uses, as far
as I can tell, iso-8859-1. This renders the above construction
useless if the user enters any character codes above 128.
Using raw_input instead of readline addresses the problem by making
it impossible to enter non-ascii text.
Please advise.
This is only a temporary problem, as eventually this application
will use Tkinter as an interface instead. But of course then I'll
probably have a bunch of new problems.