Agargara
Agargara

Reputation: 932

How to convert from a unicode code point to an actual unicode string in python?

I have a file with a list of unicode code points:

0x9C00
0x9D70
0x9D6B
0xFA2D

I'm trying to create an array of unicode characters, like so:

with open("KANJI.TXT") as f:
    kanjiCodes = f.readlines()

alphabet = [
    code_point for code_point in kanjiCodes
]

How can I convert these 0x strings to actual unicode strings?

Upvotes: 2

Views: 108

Answers (2)

Marcin
Marcin

Reputation: 238975

In python 3.x you could do as follows:

kanjiCodes=['0x9C00', '0x9D70','0x9D6B', '0xFA2D']

print([chr(int(code_point, 16)) for code_point in kanjiCodes])
# ['鰀', '鵰', '鵫', '鶴']

Upvotes: 1

Joran Beasley
Joran Beasley

Reputation: 114098

use unichr

print unichr(0x9C00)

Upvotes: 1

Related Questions