本文介绍了Python unicode 代码点到 unicode 字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一些中文、俄语或各种非英语字符集写入平面文件以进行测试.我被困在如何将 Unicode 十六进制或十进制值输出到其相应的字符上.

例如,在 Python 中,如果您有一组硬编码的字符,例如 абвгдежзийкл,您将分配 value = u"абвгдежзийкл" 没有问题.

但是,如果您在变量中存储了一个十进制或十六进制十进制,例如 1081/0439,并且您想用它对应的实际字符(而不仅仅是输出 0x439)将其打印出来,该怎么办?上面的 Unicode 十进制/十六进制值指的是 ©.

解决方案

Python 2:使用 unichr():

>>>打印(unichr(1081))©

Python 3:使用 chr():

>>>打印(chr(1081))©

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():

>>> print(unichr(1081))
й

Python 3: Use chr():

>>> print(chr(1081))
й

这篇关于Python unicode 代码点到 unicode 字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 09:36