などを試してみましたがio
、repr()
うまくいきません!
入力の問題å
( \xe5
) :
(これらはどれも機能しません)
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'))
彼らはすべて私に大まかに与えますUnicodeDecodeError: 'utf-8' codec can't decode byte 0xe5 in position 0: unexpected end of data
また、 Python を次のように起動しようとしました:export PYTHONIOENCODING=utf-8
どちらも機能しません。
今、私がいる場所は次のとおりです。
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'))
これは私に与えます:�
それは近いです...
コンソールで を取得し\xe5
て変換するにはどうすればよいですか? このソリューションはそれを壊すので、å
それも壊すことなく。input()
注:これは以前に尋ねられたことは知っていますが、それらのどれもそれを解決しません..特にioではありません
私のシステムの情報
os.environ['LANG'] == 'C'
sys.getdefaultencoding() == 'utf-8'
sys.stdout.encoding == 'ANSI_X3.4-1968'
sys.stdin.encoding == 'ANSI_X3.4-1968'
私のOS:ArchLinux
実行xterm
中 実行すると、次のことlocale -a
が得られます。C | POSIX | sv_SE.utf8
私はこれらに従いました:
- Python 3: stdin エンコーディングを指定する方法
- 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 - エンコード/デコードとバイト/文字列
- Python 3でsys.stdoutエンコーディングを設定するには?
- http://docs.python.org/3.0/howto/unicode.html
(そしてさらに数50)
解決策(一種の、まだ壊れている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'))