16進文字列は、次の'\xd3'
ように表すこともできますÓ
。
16進文字列の文字表現をコンソールに取得するために私が見つけた最も簡単な方法は次のとおりです。
print unichr(ord('\xd3'))
または、英語では、16進文字列を数値に変換し、その数値をユニコードコードポイントに変換して、最後にそれを画面に出力します。これは余分なステップのようです。もっと簡単な方法はありますか?
print u'\xd3'
あなたがしなければならないのはすべてです。どういうわけかPythonにそれがUnicodeリテラルであることを伝える必要があります。リーディングu
はそれを行います。複数のキャラクターでも機能します。
リテラルではなく変数について話している場合:
codepoints = '\xd3\xd3'
print codepoints.decode("latin-1")
編集:print
端末のエンコーディングと互換性がない場合、ing時に特定のエンコーディングを指定しても機能print
しないため、encode(sys.stdout.encoding)
自動的に指定してください。@ThomasKに感謝します。
データが「\xe0\ xa4 \ xb9 \ xe0 \ xa5 \ x88 \ xe0 \ xa4 \ xb2 \ xe0 \ xa5 \ x8b \ xe0 \ xa4 \ x95 \ xe0 \ xa4\xb2」のようなものである場合
sys.stdout.buffer.write(data)
印刷します
हैलो कल
少し前まで、私は非常によく似た問題を抱えていました。特殊文字(eg、)の代わりにUnicode 16進数(eg、 )を含むファイルをデコードする必要がありました。解決策は次のコードで説明されています。_x0023_
#
from collections import OrderedDict
import re
def decode_hex_unicode_to_latin1(string: str) -> str:
hex_unicodes = list(OrderedDict.fromkeys(re.findall(r'_x[?:\da-zA-Z]{4}_', string)))
for code in hex_unicodes:
char = bytes.fromhex(code[2:-1]).decode("latin1")[-1]
string = string.replace(code, char)
return string
def main() -> None:
string = "|_x0020_C_x00f3_digo_x0020_|"
decoded_string = decode_hex_unicode_to_latin1(string)
print(string, "-->", decoded_string)
return
if __name__ == '__main__':
main()
|_x0020_C_x00f3_digo_x0020_| --> | Código |