[英]Convert long hex to unicode character such as emoji, programmatically
Given a variable containing the hex value of an emoji character as str
(eg, s = '1f602'
), how to programmatically print that into a file as a UTF-8 encoded emoji character? 给定一个包含表情符号字符的十六进制值作为
str
的变量(例如s = '1f602'
),如何以编程方式将其作为UTF-8编码的表情符号字符打印到文件中?
This question doesn't do it programmatically, but requiring the code point itself to be included in the source code. 这个问题不是通过编程方式完成的,而是要求代码点本身包含在源代码中。
I know that this works in Python 3 only : 我知道这仅适用于Python 3 :
import codecs
s = '1f602'
with codecs.open('test.out', 'w', 'utf-8') as outfile:
outfile.write('{}\n'.format(eval('u"{}{}"'.format(r'\U000', text))))
The file, when opened in a supported text editor, will show a single emoji character. 在受支持的文本编辑器中打开文件后,该文件将显示一个表情符号字符。
How to make this works also in Python 2, and without eval
? 如何在没有
eval
情况下在Python 2中也能使它工作?
I thought unichr
would work, but it only accept unicode characters less than 0x10000
. 我以为
unichr
可以工作,但是它只接受小于0x10000
unicode字符。
You could also go through UTF-32 encoding: 您也可以通过UTF-32编码:
import struct
def fullchr(n):
return struct.pack('<I', n).decode('utf-32le')
outfile.write(fullchr(0x1F602)) # int('1F602', 16)
Or from Python 3.3 onwards there is no longer such a thing as a narrow build, so you can just use chr(0x1F602)
. 或者从Python 3.3开始,不再有狭窄的构建这样的事情,因此您可以只使用
chr(0x1F602)
。
This works in both Python 2 and 3. It uses the safer ast.literal_eval
to build the character, since as you found, unichr
won't work for characters above U+FFFF on a narrow Python 2 build. 这适用于Python 2和3。它使用更安全的
ast.literal_eval
来构建字符,因为如您ast.literal_eval
, unichr
在狭窄的Python 2构建中不适用于U + FFFF以上的字符。
import ast
import io
s = '1f602'
s2 = "u'\\U{:08X}'".format(int(s,16))
c = ast.literal_eval(s2)
with io.open('test.txt','w',encoding='utf8') as f:
f.write(c)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.