I've tried io
, repr()
etc, they don't work!
Problem inputting å
(\xe5
):
(None of these work)
import sys
print(sys.stdin.read(1))
sys.stdin = io.TextIOWrapper(sys.stdin.detach(), errors='replace', encoding='iso-8859-1', newline='\n')
print(sys.stdin.read(1))
x = sys.stdin.buffer.read(1)
print(x.decode('utf-8'))
They all give me roughly UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe5 in position 0: unexpected end of data
Also tried starting Python with: export PYTHONIOENCODING=utf-8
doesn't work either.
Now, here's where i'm at:
import sys, codecs
sys.stdout = codecs.getwriter("utf-8")(sys.stdout.detach())
sys.stdin = codecs.getwriter("utf-8")(sys.stdin.detach())
x = sys.stdin.read(1)
print(x.decode('utf-8', 'replace'))
This gives me: �
It's close...
How can i take a \xe5
and turn it into å
in my console?
Without it breaking input()
as well, because this solution breaks it.
Note: I know this has been asked before, but non of those solve it.. especially not io
Some info of my system
os.environ['LANG'] == 'C'
sys.getdefaultencoding() == 'utf-8'
sys.stdout.encoding == 'ANSI_X3.4-1968'
sys.stdin.encoding == 'ANSI_X3.4-1968'
My os: ArchLinux
running xterm
Running locale -a
gives me: C | POSIX | sv_SE.utf8
I've followed these:
- Python 3: How to specify stdin encoding
- http://python-notes.curiousefficiency.org/en/latest/python3/binary_protocols.html
- http://wolfprojects.altervista.org/talks/unicode-and-python-3/
- http://getpython3.com/diveintopython3/strings.html
- Python 3 - Encode/Decode vs Bytes/Str
- How to set sys.stdout encoding in Python 3?
- http://docs.python.org/3.0/howto/unicode.html
(and a few 50 more)
Solution (sort of, still breaks input()
)
sys.stdout = codecs.getwriter("latin-1")(sys.stdout.detach())
sys.stdin = codecs.getwriter("latin-1")(sys.stdin.detach())
x = sys.stdin.read(1)
print(x.decode('latin-1', 'replace'))