Unicode空格是否有Python常量?

Mar*_*som 18 c python string unicode whitespace

string模块包含一个whitespace属性,该属性是一个由所有被视为空格的ASCII字符组成的字符串.是否有相应的常量也包含Unicode空格,例如不间断空格(U + 00A0)?我们可以从问题" strip()和strip(string.whitespace)给出不同的结果 ",至少strip知道额外的Unicode空白字符.

这个问题被确定为 In Python[:space:]的副本,如何列出POSIX扩展正则表达式匹配的所有字符,但该问题的答案确定了搜索空白字符以生成自己的列表的方法.这是一个耗时的过程.我的问题是关于一个常数.

Aar*_*all 17

Unicode空格是否有Python常量?

简短的回答:不.我亲自在Python代码库中为这些字符(特别是数字代码点)进行了搜索,并且不存在这样的常量.

以下部分解释了为什么没有必要,以及如何在没有此信息作为常量的情况下实现它.但拥有这样一个常数也是一个非常糟糕的主意.

如果Unicode Consortium添加了另一个语义空白的字符/代码点,那么Python的维护者在继续支持语义错误的代码或更改常量以及可能破坏可能(不建议)做出假设的预先存在的代码之间将有一个糟糕的选择.关于不变的不变.

怎么能添加这些字符代码点?Unicode中有1,111,998个可能的字符.但是从版本8开始只占用了120,672个.每个新版本的Unicode都可以添加其他字符.其中一个新角色可能是一种空白形式.

信息存储在动态生成的C函数中

确定unicode中的空格是什么的代码是以下动态生成的代码.

# Generate code for _PyUnicode_IsWhitespace()
print("/* Returns 1 for Unicode characters having the bidirectional", file=fp)
print(" * type 'WS', 'B' or 'S' or the category 'Zs', 0 otherwise.", file=fp)
print(" */", file=fp)
print('int _PyUnicode_IsWhitespace(const Py_UCS4 ch)', file=fp)
print('{', file=fp)
print('    switch (ch) {', file=fp)
for codepoint in sorted(spaces):
    print('    case 0x%04X:' % (codepoint,), file=fp)
print('        return 1;', file=fp)
print('    }', file=fp)
print('    return 0;', file=fp)
print('}', file=fp)
print(file=fp)
Run Code Online (Sandbox Code Playgroud)

这是一个switch语句,它是一个常量代码块,但是这个信息不像字符串模块那样可用作模块"常量".它隐藏在从C编译的函数中,不能直接从Python访问.

这可能是因为随着更多代码点添加到Unicode,我们将无法为了向后兼容性原因而更改常量.

生成的代码

这是目前在提示中生成的代码:

int _PyUnicode_IsWhitespace(const Py_UCS4 ch)
{
    switch (ch) {
    case 0x0009:
    case 0x000A:
    case 0x000B:
    case 0x000C:
    case 0x000D:
    case 0x001C:
    case 0x001D:
    case 0x001E:
    case 0x001F:
    case 0x0020:
    case 0x0085:
    case 0x00A0:
    case 0x1680:
    case 0x2000:
    case 0x2001:
    case 0x2002:
    case 0x2003:
    case 0x2004:
    case 0x2005:
    case 0x2006:
    case 0x2007:
    case 0x2008:
    case 0x2009:
    case 0x200A:
    case 0x2028:
    case 0x2029:
    case 0x202F:
    case 0x205F:
    case 0x3000:
        return 1;
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

让自己不变:

下面的代码(从我的答案在这里),在Python 3,生成所有空白的常数:

import re
import sys

s = ''.join(chr(c) for c in range(sys.maxunicode+1))
ws = ''.join(re.findall(r'\s', s))
Run Code Online (Sandbox Code Playgroud)

作为优化,您可以将其存储在代码库中,而不是在每个新进程中自动生成它,但我会谨慎地假设它永远不会改变.

>>> ws
'\t\n\x0b\x0c\r\x1c\x1d\x1e\x1f \x85\xa0\u1680\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u2028\u2029\u202f\u205f\u3000'
Run Code Online (Sandbox Code Playgroud)

(链接问题的其他答案显示了如何为Python 2获得该问题.)

请记住,有一次,有些人可能认为256个字符编码就是我们所需要的.

>>> import string
>>> string.whitespace
' \t\n\r\x0b\x0c'
Run Code Online (Sandbox Code Playgroud)

如果您坚持在代码库中保持常量,只需为您的Python版本生成常量,并将其存储为文字:

unicode_whitespace = u'\t\n\x0b\x0c\r\x1c\x1d\x1e\x1f \x85\xa0\u1680\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u2028\u2029\u202f\u205f\u3000'
Run Code Online (Sandbox Code Playgroud)

u前缀使得它在Python 2 UNICODE(2.7恰好识别为空白太上方的整个字符串),并在Python 3作为字符串文字是由缺省Unicode它被忽略.