我有一個Unicode字符串,其開頭和結尾都有一些非空白空格。當使用strip()
與strip(string.whitespace)
時,我會得到不同的結果。strip()和strip(string.whitespace)給出了不同的結果,儘管文檔提示它們應該是相同的
>>> import string
>>> s5 = u'\xa0\xa0hello\xa0\xa0'
>>> print s5.strip()
hello
>>> print s5.strip(string.whitespace)
hello
爲strip()
文檔說,「如果省略或None
,所述chars
參數默認爲刪除空白」。 string.whitespace
的文檔稱「包含所有被認爲是空白的字符的字符串」。
因此,如果string.whitespace
包含所有被認爲是空白的字符,那麼爲什麼結果會不同呢?它與Unicode有什麼關係?
我使用Python 2.7.6
'string.whitespace'在我的Python 3.2.3上是''\ t \ n \ r \ x0b \ x0c「'。顯然Unicode不在圖片中。 –
但是,文檔並沒有說'unicode.strip'使用'string.whitespace'來定義什麼是和不是空白。我相信'string'模塊的大部分已被棄用,已被摺疊到'str'類中。 – chepner
@chepner除了常量,模板,格式化程序和maketrans - y ...幾乎所有的課上現在 –