Python 尽管文档表明它们应该相同,strip() 和 strip(string.whitespace) 给出了不同的结果
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22230080/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
strip() and strip(string.whitespace) give different results despite documentation suggesting they should be the same
提问by Becca codes
I have a Unicode string with some non-breaking spaces at the beginning and end. I get different results when using strip()vs. strip(string.whitespace).
我有一个 Unicode 字符串,开头和结尾都有一些不间断的空格。使用strip()vs.时我得到不同的结果strip(string.whitespace)。
>>> import string
>>> s5 = u'\xa0\xa0hello\xa0\xa0'
>>> print s5.strip()
hello
>>> print s5.strip(string.whitespace)
??hello??
The documentation for strip()says, "If omitted or None, the charsargument defaults to removing whitespace." The documentation for string.whitespacesays, "A string containing all characters that are considered whitespace."
的文档strip()说,“如果省略 or None,则chars参数默认为删除空格。” 文档string.whitespace说,“包含所有被视为空格的字符的字符串。”
So if string.whitespacecontains all characters that are considered whitespace, then why are the results different? Does it have something to do with Unicode?
因此,如果string.whitespace包含所有被视为空白的字符,那么为什么结果不同?它与Unicode有关吗?
I am using Python 2.7.6
我正在使用 Python 2.7.6
采纳答案by Bakuriu
From the documentation of the string.whitespace:
从文档中string.whitespace:
A string containing all ASCII charactersthat are considered whitespace. This includes the characters space, tab, linefeed, return, formfeed, and vertical tab.
包含所有被视为空白的ASCII 字符的字符串。这包括字符空格、制表符、换行符、回车、换页和垂直制表符。
It's the same under python3, where all non-ASCII constants where removed. (In python2 some constants could be influenced by localesettings).
在python3下也是一样,所有非ASCII常量都被删除了。(在 python2 中,一些常量可能会受到locale设置的影响)。
Hence the difference in behaviour is quite obvious since strip()doesremove any unicodewhitespace, while strip(string.whitespace)removes only ASCII spaces. Your string clearly contains non-ASCII spaces.
因此行为上的差异非常明显,因为strip()确实删除了任何unicode空格,而strip(string.whitespace)只删除了 ASCII 空格。您的字符串显然包含非 ASCII 空格。

