[英]readlines not reading the last line of the file in python
I have a code where I am reading all the lines from the file using readlines function and I am further parsing each line in a list. 我有一个代码,我正在使用readlines函数从文件中读取所有行,我正在进一步解析列表中的每一行。 But when I printed the list I saw that the loop is ignoring the last line in the file.
但是当我打印列表时,我看到循环忽略了文件中的最后一行。 When I inserted a blank line in the file then all the contents are read.
当我在文件中插入一个空行时,将读取所有内容。 can you pls tell me why it is doing that
你能不能告诉我它为什么这样做
def readFile1(file1):
f = file1.readlines()
cList1 = []
for line in f:
if re.findall('\n',line):
v = re.sub('\n','',line)
cList1.append(v)
print cList1
This is printing all the contents except the last line of the file. 这是打印除文件最后一行之外的所有内容。
If the last line doesn't end with a newline, your code won't add it to cList1
. 如果最后一行没有以换行符结尾,则代码不会将其添加到
cList1
。 Instead, it would add a second copy of the penultimate line (which is still stored in v
). 相反,它会添加倒数第二行的第二个副本(仍然存储在
v
)。
A cleaner way to write that loop is: 编写该循环的更简洁方法是:
cList1 = []
for line in f:
cList1.append(line.rstrip('\n'))
Or, indeed: 或者,确实:
cList1 = [line.rstrip('\n') for line in f]
In fact, I would avoid the readlines()
call entirely: 事实上,我会完全避免
readlines()
调用:
def readFile1(file1):
cList1 = [line.rstrip('\n') for line in file1]
print cList1
If you just want to get all lines from a file into a list, there's a much easier (and cleaner, in my opinion) way. 如果您只想将文件中的所有行都放到列表中,那么就会更容易(在我看来更清晰)。
def readFile1(file1):
cList1 = file1.read().splitlines()
print cList1
I don't think there's any need to use a generator in this case. 在这种情况下,我认为不需要使用发电机。 Also, I benchmarked it (on Windows) and the generator form that @aix gave is slightly slower in some cases .
另外,我对它进行了基准测试(在Windows上),@ aix给出的生成器形式在某些情况下稍慢。
>>> import timeit
>>> import os
>>>
>>> # Setup
>>> open('testfile', 'w').write('This Is A Test' * 500)
>>>
>>> # Time generator form (ten thousand times)
>>> timeit.timeit("lst = [line.rstrip('\\n') for line in open('testfile')]",
... number=10000)
2.656837282256163
>>>
>>> # Time splitlines() form (ten thousand times)
>>> timeit.timeit("lst = open('testfile').read().splitlines()", number=10000)
1.3464799954204238
>>>
>>> # Cleanup
>>> os.remove('testfile')
您的最后一行没有\\ n字符,因为您之后没有新行。
print f
actually prints all lines. print f
实际打印所有行。 It's a bug in your code. 这是你代码中的一个错误。 You append the second-to-last line twice, since the last line does not contain
\\n
. 您将倒数第二行追加两次,因为最后一行不包含
\\n
。 You're missing eg an else
block that assign v
when it doesn't contain a \\n
. 你错过了一个
else
块,当它不包含\\n
时分配v
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.