I'm trying to write out to a flat file some Chinese, or Russian or various non-English character-sets for testing purposes. I'm getting stuck on how to output a Unicode hex-decimal or decimal value to its corresponding character.
For example in Python, if you had a hard coded set of characters like абвгдежзийкл
you would assign value = u"абвгдежзийкл"
and no problem.
If however you had a single decimal or hex decimal like 1081 / 0439 stored in a variable and you wanted to print that out with it's corresponding actual character (and not just output 0x439) how would this be done? The Unicode decimal/hex value above refers to й
.
Python 2: Use
unichr()
:Python 3: Use
chr()
:So the answer to the question is:
int(hex_value, 16)
chr()
.To sum up:
If you run into the error:
While trying to convert your hex value using
unichr
, you can get around that error by doing something like: