splitlines Knows More Line Breaks

splitlines recognizes every Unicode line boundary — CRLF, NEL, and line separators included — while a plain split on newline leaves carriage returns behind.

PythonData Integrity

Code

pythonsnippet
11 lines
# 用 chr() 构造真实的 Unicode 行边界字符,避免源码里出现转义序列
CR, LF, NEL, LS = chr(13), chr(10), chr(133), chr(8232)
text = "alpha" + CR + LF + "beta" + LF + "gamma" + NEL + "delta" + LS + "epsilon"

# split 只认给定的一个分隔符:CRLF 里的 CR 残留,NEL/LS 根本不拆
print("split:     ", repr(text.split(LF)))

# splitlines 认识全部 Unicode 行边界:CRLF、NEL(133)、LS(8232) 都算
print("splitlines:", repr(text.splitlines()))

print("manual count:", text.count(LF), "vs splitlines:", len(text.splitlines()))

Output

Captured from a sandboxed run of this snippet in the build pipeline.

split:      ['alpha\r', 'beta', 'gamma\x85delta\u2028epsilon']
splitlines: ['alpha', 'beta', 'gamma', 'delta', 'epsilon']
manual count: 2 vs splitlines: 5

How it works

Top-level bindings text hold the demo state — nothing sits inside a function scope.

Using it

The demo output is labeled (split:, splitlines:, manual count:), so every printed line maps back to its originating operation.

Interactive Tools for This Topic

FAQ

Is the dependency story complicated?
Not at all: zero dependencies, so there is nothing to version or audit. Its state lives in module-level bindings: `text`.