当前位置: 首页 > 面试题库 >

如何控制包含东亚字符的Unicode字符串的填充

楚瑞
2023-03-14
问题内容

我遇到了三个UTF-8刺痛:

hello, world
hello, 世界
hello, 世rld

我只想要前10个ascii-char-width,这样一栏中的括号即可:

[hello, wor]
[hello, 世 ]
[hello, 世r]

控制台中:

width('世界')==width('worl')
width('世 ')==width('wor')  #a white space behind '世'

一个中文字符是三个字节,但是在控制台中显示时,它只有2个ascii字符宽度:

>>> bytes("hello, 世界", encoding='utf-8')
b'hello, \xe4\xb8\x96\xe7\x95\x8c'

format()当UTF-8字符混入时,python并没有帮助

>>> for s in ['[{0:<{1}.{1}}]'.format(s, 10) for s in ['hello, world', 'hello, 世界', 'hello, 世rld']]:
...    print(s)
...
[hello, wor]
[hello, 世界 ]
[hello, 世rl]

这不是很漂亮:

-----------Songs-----------
|1: Butterfly|
|2: city of heart|
|3: support your lover|
|4: root seeds|
|5: currucucu Palo|
|6: between woodlands|
|7: blue light|
|8: in your eyes|
|9: Chopin’s farewell song|
|10: Journey to the West|
|X 11: deep in love|
|X 12: the Mo run air|
|X 13: time goes by|
|X 14: Canon|
|X 15: Serenade|
|X 16: sweet lullaby|
---------------------------

因此,我想知道是否有标准的方法来执行UTF-8填充人员?


问题答案:

尝试以固定宽度字体将ASCII文本与中文对齐时,存在一组可打印ASCII字符的全角版本。下面我制作了一张ASCII到全角版本的转换表:

    # coding: utf8

    # full width versions (SPACE is non-contiguous with ! through ~)
    SPACE = '\N{IDEOGRAPHIC SPACE}'
    EXCLA = '\N{FULLWIDTH EXCLAMATION MARK}'
    TILDE = '\N{FULLWIDTH TILDE}'

    # strings of ASCII and full-width characters (same order)
    west = ''.join(chr(i) for i in range(ord(' '),ord('~')))
    east = SPACE + ''.join(chr(i) for i in range(ord(EXCLA),ord(TILDE)))

    # build the translation table
    full = str.maketrans(west,east)

    data = '''\
Butterfly (a song)
Another song
Support your lover (yet another song)
Rooted seeds
Cucurrucucu Palo whatever
Between woodlands
Blu ray
In your eyes
Chopin's farewell
Journey to the West
Deep in love
Love the earth
Time goes by
Cannon
Serenade by Schubert
Sweet lullaby
    '''

    # Replace the ASCII characters with full width, and create a song list.
    data = data.translate(full).rstrip().split('\n')

    # translate each printable line.
    print(' ----------Songs-----------'.translate(full))
    for i,song in enumerate(data):
        line = '|{:4}: {:20.20}|'.format(i+1,song)
        print(line.translate(full))
    print(' --------------------------'.translate(full))

输出量

 ----------Songs-----------
|   1: Butterfly (asong)                 |
|   2: anothersong                             |
|    3: support your lovers                        |
|   4:the root seeds               |
|   5:cucurrucupalo                             |
|   between woodlands               |
|   7: Blu ray                 |
|   8: in your eyes              |
|   9: Chopin's farewell song         |
|  10: Journey to the West                     |
|  11: deep in love               |
|  12: love the earth             |
|  13: time goes by              |
|  14: Canon                  |
|  15: Serenade                            |
|  16: sweet lullaby                           |
 --------------------------

它不是太漂亮,但是排列整齐。



 类似资料:
  • 问题内容: 我有一个字符串存储为: 有没有办法我可以转换成? 问题答案: 您可以为此使用: 输出:

  • 问题内容: Java中的String数据类型使我们可以通过codePointCount知道一个字符串中有多少个Unicode字符。以及如何通过codePointAt获取第n个unicode char。我很想知道是否有一个API来获取包含Java中前N个unicode字符的子字符串。 谢谢, 问题答案: 没有一个方法可以一次调用,但可以帮助您完成此操作。

  • 问题内容: 如何将Unicode字符串(包含额外的字符,如£$等)转换为Python字符串? 问题答案:

  • 我有一个字符串返回给我,其中包含转义字符。 这是一个示例字符串 " test\40gmail.com " 如您所见,它包含转义字符。我需要将其转换为其实际值,即 " test@gmail.com " 我该怎么做?

  • 问题内容: 我从HTML页面中将一个字符串输入到Java HTTPServlet中。根据我的要求,我得到了显示汉字的ASCII码: “&#21487;&#20197;&#21578;&#35785;&#25105;” (无空格) 如何将该字符串转换为Unicode? HTML代码: Java代码: 如果我打印问题[0],则会得到以下值:“&#21487;&#20197;&#21578;&#3578

  • 我在Mako模板中有以下代码: 这将url转义应用于表示公司的对象的名称字符串。然后在URL中使用得到的转义字符串。Mako文档说明使用提供url编码。 在服务器上,我将公司名称部分接收到参数中: 然后使用investment_name作为键返回到模板中提取它的字典中。 这对于公司名称中的所有标准大小写(如空格、斜杠和单引号)都很适用。但是,如果公司名称包含ascii字符集之外的unicode字符