社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

如何在python中获得utf-16(十进制)?

Newbie • 7 年前 • 2578 次点击  

我有一个表示为 U+1F498型 :

emoticon = u'\U0001f498'

我想得到这个字符的utf-16十进制组,根据 this website 是 55357 和 56472 .

我试着去做 print emoticon.encode("utf16") 但一点也帮不了我,因为它给了我一些其他的角色。

另外,在编码到utf-16之前尝试从utf-8解码,如下所示 print str(int("0001F498", 16)).decode("utf-8").encode("utf16") 也无济于事。

如何正确获取unicode字符的utf-16十进制组?

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/39672
文章 [ 2 ]  |  最新文章 7 年前
Mark Tolonen
Reply   •   1 楼
Mark Tolonen    8 年前

在Python2“窄”构建中,它非常简单:

>>> emoticon = u'\U0001f498'
>>> map(ord,emoticon)
[55357, 56472]

这在Python2(窄和宽版本)和Python3中都适用:

from __future__ import print_function
import struct

emoticon = u'\U0001f498'
print(struct.unpack('<2H',emoticon.encode('utf-16le')))

输出:

(55357, 56472)

这是一个更通用的解决方案,可以为任意长度的字符串打印utf-16代码点:

from __future__ import print_function,division
import struct

def utf16words(s):
    encoded = s.encode('utf-16le')
    num_words = len(encoded) // 2
    return struct.unpack('<{}H'.format(num_words),encoded)

emoticon = u'ABC\U0001f498'
print(utf16words(emoticon))

输出:

(65, 66, 67, 55357, 56472)
Aran-Fey
Reply   •   2 楼
Aran-Fey    8 年前

你可以 encode 角色与 utf-16 编码,然后将每2个字节的编码数据转换为 int.from_bytes (或) struct.unpack 在python 2中)。

蟒蛇3

def utf16_decimals(char, chunk_size=2):
    # encode the character as big-endian utf-16
    encoded_char = char.encode('utf-16-be')

    # convert every `chunk_size` bytes to an integer
    decimals = []
    for i in range(0, len(encoded_char), chunk_size):
        chunk = encoded_char[i:i+chunk_size]
        decimals.append(int.from_bytes(chunk, 'big'))

    return decimals

python 2+python 3

import struct

def utf16_decimals(char):
    # encode the character as big-endian utf-16
    encoded_char = char.encode('utf-16-be')

    # convert every 2 bytes to an integer
    decimals = []
    for i in range(0, len(encoded_char), 2):
        chunk = encoded_char[i:i+2]
        decimals.append(struct.unpack('>H', chunk)[0])

    return decimals

结果:

>>> utf16_decimals(u'\U0001f498')
[55357, 56472]