2

File abc's content:

a
b
c

The code is

data_fh = open("abc")
str = data_fh.read()
arr = str.split("\n")
print len(arr)
data_fh.seek(0)
arr = data_fh.read().splitlines()
print len(arr)

but the output is:

4
3

so why is that?

4

2 回答 2

5

因为.splitlines()不包括末尾的空行,while.split('\n')返回最后一个空字符串...\n

>>> 'last\n'.split('\n')
['last', '']
>>> 'last\n'.splitlines()
['last']

这在str.splitlines()文档中明确提到:

与给定split()分隔符字符串sep不同,此方法为空字符串返回一个空列表,并且终端换行符不会导致额外的行。

如果没有尾随换行符,则输出相同:

>>> 'last'.split('\n')
['last']
>>> 'last'.splitlines()
['last']

换句话说,str.split()不添加任何东西,但str.splitlines()确实删除。

于 2013-08-08T14:13:51.987 回答
4

You probably have a trailing newline:

>>> s = 'a\nb\nc\n'  # <-- notice the \n at the end
>>>
>>> s.split('\n')
['a', 'b', 'c', '']
>>>
>>> s.splitlines()
['a', 'b', 'c']

Notice that split() leaves an empty string at the end whereas splitlines() does not.

As an aside, you shouldn't use str as a variable name since that's already taken by a built-in function.

于 2013-08-08T14:13:44.950 回答