文本文件行数 - python2 与 python3 中的数字不同

问题描述 投票:0回答:0

我正在使用此命令读取文本文件:

print(len(list(open(filename))))

现在,当我在 Python2 中运行它时,我得到 5622862 行,但是当我用 Python3 读取它时,我得到 5622865 行。怎么会这样?顺便说一句,当我在命令行中做

cat file.txt | wc -l
我得到与Python2相同的结果。

这让我抓狂。应该具有相同行长度 (5622862) 的两个文件在 python2 中确实具有相同的长度,但在 python3 中则不同(5622862 与 5622865)。会不会是python3有bug?

在 python3 中进行不同的读取也不起作用:

list(open(filename, "r", encoding="utf-8"))
python python-3.x text-files python-2.x line-count
© www.soinside.com 2019 - 2024. All rights reserved.