LoginSignup
0
0

More than 1 year has passed since last update.

Pythonで 'U+5B57' <--> '字' 変換

Last updated at Posted at 2021-10-19

なに

Pythonで 'U+5B57'<-->'字'のような、'U+xxxx'という文字列とそれが指し示す文字の相互変換を行います。

ここで注意なのが、「python Unicode 文字列 変換」とかでググるとあまた出てくるunicode-escapeうんぬんの話ではないです。

変換対象

'字' ... 文字列
'U+5B57' ... 文字列

方法

  • 'U+xxxx' --> 文字
u = 'U+5B57'
print(chr(int(u[2:], 16)))
# output: 字
  • 文字 --> 'U+xxxx'
kanji = "字"
print(f"U+{hex(ord(kanji))[2:].upper()}")
# output: U+5B57

おわり

以上です。
お疲れ様でした。

0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0