相关疑难解决方法(0)

如何在Python 3中迭代Unicode字符?

我需要一次遍历Python字符串一个字符,但是一个简单的"for"循环代替我给UTF-16代码单元:

str = "abc\u20ac\U00010302\U0010fffd"
for ch in str:
    code = ord(ch)
    print("U+{:04X}".format(code))
Run Code Online (Sandbox Code Playgroud)

打印:

U+0061
U+0062
U+0063
U+20AC
U+D800
U+DF02
U+DBFF
U+DFFD
Run Code Online (Sandbox Code Playgroud)

当我想要的是:

U+0061
U+0062
U+0063
U+20AC
U+10302
U+10FFFD
Run Code Online (Sandbox Code Playgroud)

有没有办法让Python给我一系列Unicode代码点,无论字符串是如何在引擎盖下实际编码的?我在这里测试Windows,但我需要能在任何地方使用的代码.它只需要在Python 3上工作,我不关心Python 2.x.

到目前为止我能想出的最好的是:

import codecs
str = "abc\u20ac\U00010302\U0010fffd"
bytestr, _ = codecs.getencoder("utf_32_be")(str)
for i in range(0, len(bytestr), 4):
    code = 0
    for b in bytestr[i:i + 4]:
        code = (code << 8) + b
    print("U+{:04X}".format(code))
Run Code Online (Sandbox Code Playgroud)

但我希望有一种更简单的方法.

(对精确的Unicode术语进行迂腐的挑剔将以四个线索无情地击败头部.我想我已经明确了我在这之后的事情,请不要浪费空间"但是UTF-16是从技术上讲,Unicode也是"一种论点."

python unicode python-3.x

13
推荐指数
1
解决办法
3994
查看次数

标签 统计

python ×1

python-3.x ×1

unicode ×1