Pythonでは、特定の範囲のUnicode文字を取得するためにunichr()
関数を使用できます。この関数は、指定した範囲の整数値(この場合は1000から1100)をUnicode文字に変換します。Python 3では、chr()
関数が同じ目的で使用できます。
以下に、PythonでUTF-8の全ての文字を取得するための基本的なコードスニペットを示します。
# Python 2
for i in range(1000, 1100):
print i, unichr(i)
# Python 3
for i in range(1000, 1100):
print(i, chr(i))
このコードは、1000から1100までの範囲の整数値を取り、それぞれの整数値を対応するUnicode文字に変換します。
また、任意のUnicode範囲の文字を取得するためのPython 3のコードもあります。
start_code, stop_code = '4E00', '9FFF' # CJK Unified Ideographs
start_idx, stop_idx = [int(code, 16) for code in (start_code, stop_code)] # from hexadecimal to unicode code point
characters = []
for unicode_idx in range(start_idx, stop_idx+1):
characters.append(chr(unicode_idx))
このコードは、指定した範囲のUnicode文字を取得し、それらをリストに追加します。
以上の情報がPythonでUTF-8文字を全て取得する方法についての理解に役立つことを願っています。