とあるAPIを利用したところ、
'\uXXXX\uXXXX' のような文字列がかえって来ました。
※ XXXXの部分は4桁の16進数です。
この状態では読めないため、普通の文字に変換したいです。
Python3系で変換するには、どうすれば良いでしょうか。
宜しくお願い致します。
おかげさまで以下コードにてやりたいこと出来ました!
python
1i, s, name = 0, '', <APIから返ってきた文字列> 2while i < len(name): 3 if name[i] == '\': 4 s += chr(int(name[i+2:i+6], 16)) 5 i+=5 6 else: 7 s += name[i] 8 9 i += 1 10print(s)
が、そもそも以下関数を利用し、自分でエンコードしていました。。。
(sampleスクリプトを流用し、APIのエンドポイントのみ変更していたため、気づきませんでした)
python
1def _encode_for_display(text): 2 """ 3 Encodes strings so they can display as ASCII in a Windows terminal window. 4 This function also encodes strings for processing by xml.etree.ElementTree functions. 5 6 Returns an ASCII-encoded version of the text. 7 Unicode characters are converted to ASCII placeholders (for example, "?"). 8 """ 9 return text.encode('ascii', errors="backslashreplace").decode('utf-8')
皆様ご回答ありがとうございました。
問題の文字列にはU+10000以上のコードポイントの文字が含まれていそうかどうかわかりますか?
回答3件
あなたの回答
tips
プレビュー