【问题标题】:How to convert string of caps to unicode bold caps如何将大写字符串转换为 unicode 粗体大写
【发布时间】:2021-10-05 01:58:46
【问题描述】:

我想以编程方式将一串大写字母(例如 "ABC")转换为以 unicode 粗体大写字母表示相同的字符串(在 "\U0001d400" 处以粗体 A 开头)

我可以在 26 个 unicode 的元组中逐个字符地查找:

x='AB'
bold_unicodes = ( '\U0001d400', '\U0001d401', ..., '\U0001d419')
result = []
for c in x:
    result.append( bold_unicodes[ ord(c)-ord('A')] )
result = ''.join(result)

print(result) 产生 ???????? (是的,我可以将 unicode 复制/粘贴到 StackOverflow 中!)

但是,这真的是最好的方法吗?是不是最清楚?

为了让自己了解bytes 和unicode 的奥秘,有没有办法将整数偏移量ord(c)-ord('A') 与代表26 个连续unicode 基点的整数0x1d400 结合起来?因为小写字母和数字有许多类似的转换,包括粗体、斜体和其他字体。

【问题讨论】:

    标签: python python-3.x python-unicode


    【解决方案1】:

    您可以将 unicode 块之间的偏移量相加。在您的情况下,偏移量是119743(基本上是ord(bold_unicodes[0])-ord('A')),因此chr(ord(char)+119743) 返回与char 等效的粗体字。您可以对其他转换执行相同的操作。

    一个警告:这可能很明显,但您需要确保输入字符位于正确的块中,以免得到任何意外输出 - 例如,chr(ord('a')+119743) 返回小写粗体“?”。

    【讨论】:

    • 谢谢。 (踢我自己,我为什么想不到这个?我猜是某种 ASCII 字节条件。)
    猜你喜欢
    • 2013-08-02
    • 2018-09-16
    • 2017-01-26
    • 2014-06-18
    • 2021-06-11
    • 1970-01-01
    • 1970-01-01
    • 2014-06-05
    相关资源
    最近更新 更多