[英]Unexpected split() behaviour Python
Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 26 2016, 10:47:25)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 'Checksum\t\x85\r\n'.split()
['Checksum']
標簽后面的部分在哪里?
但是,這些工作如預期的那樣:
>>> 'Checksum\t\x84\r\n'.split()
['Checksum', '\x84']
>>> 'Checksum\t\x85\r\n'.split('\t')
['Checksum', '\x85\r\n']
>>> 'Checksum\t\x84\r\n'.split('\t')
['Checksum', '\x84\r\n']
Python 3.6.6上的結果相同。
有趣的是,它在Python 2.7.10上運行良好
Python 2.7.10 (default, Aug 17 2018, 17:41:52)
[GCC 4.2.1 Compatible Apple LLVM 10.0.0 (clang-1000.0.42)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 'Checksum\t\x85\r\n'.split()
['Checksum', '\x85']
>>> 'Checksum\t\x84\r\n'.split()
['Checksum', '\x84']
因為它被視為空白:
>>> "\x85".isspace()
True
沒有參數的str.split()
根據所有空白字符刪除,它還會在拆分時刪除“空字段”
所以所有的空白字符都被刪除了。 \\x84
不是空格,所以保留:
>>> "\x84".isspace()
False
split()
沒有參數在空格上分割。 \\x85
是空白; 這是U + 0085 NEXT LINE(NEL) 。
Python 2版本的工作方式與您期望的方式相同,因為Python 2 str.split
僅計算ASCII空白。 如果您在Python 2上使用了Unicode字符串,那么您會看到相同的行為。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.