Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python unicode codepoint to unicode character

Tags:

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 й.

like image 780
stoneferry Avatar asked May 23 '12 07:05

stoneferry


People also ask

How will you convert an integer to an Unicode character in Python?

unichr() is named chr() in Python 3 (conversion to a Unicode character).

What is Codepoint in Python?

A code point value is an integer in the range 0 to 0x10FFFF (about 1.1 million values, the actual number assigned is less than that). In the standard and in this document, a code point is written using the notation U+265E to mean the character with value 0x265e (9,822 in decimal).

How do you get the UTF-8 character code in Python?

UTF-8 is a variable-length encoding, so I'll assume you really meant "Unicode code point". Use chr() to convert the character code to a character, decode it, and use ord() to get the code point. In Python 2, chr only supports ASCII, so only numbers in the [0.. 255] range.

How do I fix encoding in Python?

The best way to attack the problem, as with many things in Python, is to be explicit. That means that every string that your code handles needs to be clearly treated as either Unicode or a byte sequence. The most systematic way to accomplish this is to make your code into a Unicode-only clean room.


1 Answers

Python 2: Use unichr():

>>> print(unichr(1081)) й 

Python 3: Use chr():

>>> print(chr(1081)) й 
like image 172
NPE Avatar answered Oct 12 '22 10:10

NPE