ランダムなUnicodeの文字を一つ表示するプログラム
import unicodedata
import random
def uni():
while True:
n = random.randint(0,0x10FFFF)
if not unicodedata.category(chr(n)) in ["Cn", "Co"]:
break
return chr(n)
print(uni())
解説
unicodedata.category()は、文字を入れるとUnicodeのカテゴリー名が返ってくる関数で、カテゴリー名にはLo(普通の文字)、Nl(数字)、Cc(制御文字)などがある。上のコード例では、Cn(未定義)やCo(私用領域)以外の文字が出てくるまで、Unicodeの最大範囲(0から0x10FFFF、両端含む)でランダムな整数を作って無限ループしている。
カテゴリー名の一覧(公式)
https://www.compart.com/en/unicode/category
カテゴリー名の一覧(日本語)
https://tool-support.renesas.com/autoupdate/support/onlinehelp/ja-JP/csp/V4.01.00/CS+.chm/Editor.chm/Output/ed_RegularExpressions4-nav-3.html