Bec*_*des 7 python unicode whitespace strip
我有一个Unicode字符串,在开头和结尾有一些不间断的空格.使用strip()vs. 时我会得到不同的结果strip(string.whitespace).
>>> import string
>>> s5 = u'\xa0\xa0hello\xa0\xa0'
>>> print s5.strip()
hello
>>> print s5.strip(string.whitespace)
hello
Run Code Online (Sandbox Code Playgroud)
文档strip()说,"如果省略或None,chars参数默认删除空格." 文档string.whitespace说"包含所有被认为是空格的字符的字符串".
因此,如果string.whitespace包含所有被视为空格的字符,那么为什么结果会有所不同?它与Unicode有关吗?
我使用的是Python 2.7.6
Bak*_*riu 11
从以下文件string.whitespace:
包含所有被视为空格的ASCII字符的字符串.这包括字符空间,制表符,换行符,返回页面,换页符和垂直选项卡.
在python3下也是如此,其中删除了所有非ASCII常量.(在python2中,一些常量可能受locale设置的影响).
因此,在行为的差异还是比较明显的,因为strip() 不删除任何Unicode的空格,而strip(string.whitespace)只去除ASCII空格.您的字符串显然包含非ASCII空格.