Triyng,使這個在Python 2.7:Python的unicode.splitlines()觸發的非EOL字符
>>> s = u"some\u2028text"
>>> s
u'some\u2028text'
>>> l = s.splitlines(True)
>>> l
[u'some\u2028', u'text']
\u2028
是左到右的嵌入字符,而不是\r
或\n
,所以該行不應該被分割。有錯誤還是隻是我的誤解?
Triyng,使這個在Python 2.7:Python的unicode.splitlines()觸發的非EOL字符
>>> s = u"some\u2028text"
>>> s
u'some\u2028text'
>>> l = s.splitlines(True)
>>> l
[u'some\u2028', u'text']
\u2028
是左到右的嵌入字符,而不是\r
或\n
,所以該行不應該被分割。有錯誤還是隻是我的誤解?
\u2028
是行分隔符,左到右的嵌入是\u202A
:
>>> import unicodedata
>>> unicodedata.name(u'\u2028')
'LINE SEPARATOR'
>>> unicodedata.name(u'\u202A')
'LEFT-TO-RIGHT EMBEDDING'
碼點的名單被認爲換行符是容易的(不是那麼容易,雖然找到)在Python源看到( Python 2.7版,通過我的意見):
/* Returns 1 for Unicode characters having the line break
* property 'BK', 'CR', 'LF' or 'NL' or having bidirectional
* type 'B', 0 otherwise.
*/
int _PyUnicode_IsLinebreak(register const Py_UNICODE ch)
{
switch (ch) {
// Basic Latin
case 0x000A: // LINE FEED
case 0x000B: // VERTICAL TABULATION
case 0x000C: // FORM FEED
case 0x000D: // CARRIAGE RETURN
case 0x001C: // FILE SEPARATOR
case 0x001D: // GROUP SEPARATOR
case 0x001E: // RECORD SEPARATOR
// Latin-1 Supplement
case 0x0085: // NEXT LINE
// General punctuation
case 0x2028: // LINE SEPARATOR
case 0x2029: // PARAGRAPH SEPARATOR
return 1;
}
return 0;
}
謝謝你提到這個偉大的模塊。 – Pehat
U+2028
是LINE SEPARATOR
。 U+2028
和U+2029
(PARAGRAPH SEPARATOR
)應將視爲換行符,因此Python正在做正確的事情。
當然,在非標準的換行字符列表上拆分有時候是完全合理的。但是你不能用splitlines
這樣做。您將不得不使用split
- 如果您需要splitlines
的附加功能,則必須自行實施。例如:
return [line.rstrip(sep) for line in s.split(sep)]
'\ u2028'是'LINE SEPARATOR',左到右的嵌入是'\ u202A' –
@帕維爾 - anossov謝謝。現在我打破了在Windows charmap中查找Unicode字符的習慣。 – Pehat
有趣,我的charmap很好:http://i.imgur.com/jwkyMEm.png –