python:文件I/O统计字符不包括换行符

1 投票
6 回答
6814 浏览
提问于 2025-04-18 00:29

我有一个名为 number.txt 的文本文件,里面的内容是:

0
1
2
3

我的代码:

def main():
   inFile = open("number.txt", "r")
   text = inFile.read() 
   inFile.close()
   print(len(text))
main()

我试着用上面的代码来打印文件里有多少个字符。结果打印出来是 8,但实际上只有 4 个字符。我知道在 Python 读取文件时,会在每行后面加一个换行符,这可能就是多出来的字符。请问我该怎么去掉这个换行符呢?

6 个回答

0

试试这个:

if __name__ == '__main__':
    with open('number.txt', 'rb') as in_file:
        print abs(len(in_file.readlines()) - in_file.tell())
0

你的脚本结果是对的:其实换行符也是一种字符(只是它们是看不见的!)

如果你想去掉换行符(在字符串中用 \n\r\n 表示),你需要把它们替换成一个空字符串。

看看这段代码:

def main():
   inFile = open("number.txt", "r")
   text = inFile.read()
   text = text.replace("\r\n","") #in windows, new lines are usually these two 
   text = text.replace("\n","")   

字符。 inFile.close() print(len(text)) main()

想了解更多关于 \r\n\n 的信息,可以试试这个链接:http://en.wikipedia.org/wiki/Newline

0

使用 string.rstrip('\n')。这个方法会把字符串右边的换行符去掉,而不会影响其他部分。需要注意的是,不管你用什么操作系统,Python都会把所有的换行符转换成 \n。我还建议你逐行读取文件,而不是一次性把整个文件都加载到内存中,特别是当文件很大的时候。

示例代码:

if __name__ == '__main__':
   count = 0
   with open("number.txt", "r") as fin):
       for line in fin:
           text = line.rstrip('\n')
           count += len(text)
   print(count)
1

正如安东尼奥在评论中提到的,文件里有换行符。如果你想的话,可以把它们去掉:

def main():
   inFile = open("number.txt", "r")
   text = inFile.read() 
   inFile.close()
   text = text.replace('\n', '')  # Replace new lines with nothing (empty string).
   print(len(text))
main()
4

这个文件的每一行之间都有一个换行符。要去掉这些换行符,你可以用 replacesplit 或类似的方法重新创建一个没有换行符的字符串,或者你也可以计算一下换行符的数量,然后从总长度中减去它们的数量(这样做更快、更有效)。

with open("number.txt", "r") as file:
    text = file.read()
length_without_newlines = len(text) - text.count('\n')

补充一下:正如 @lvc 所说,Python 会把所有的换行符都转换成 '\n'(0x0A),包括 Windows 系统的换行符('\r\n' 或 [0x0D, 0x0A]),所以在查找换行符的时候,只需要找 '\n' 就可以了。

撰写回答