计算文件中的字符和行数 python 2.7 [英] counting characters and lines from a file python 2.7

查看:33
本文介绍了计算文件中的字符和行数 python 2.7的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个程序,它计算作为输入给出的文件中的所有行、单词和字符.

I'm writing a program that counts all lines, words and characters from a file given as input.

import string

def main():
    print "Program determines the number of lines, words and chars in a file."
    file_name = raw_input("What is the file name to analyze? ")

    in_file = open(file_name, 'r')
    data = in_file.read()

    words = string.split(data)

    chars = 0
    lines = 0
    for i in words:
        chars = chars + len(i)

    print chars, len(words)


main()

某种程度上,代码是可以的.

To some extent, the code is ok.

但是我不知道如何计算文件中的空格".我的字符计数器只计算字母,不包括空格.
另外,我在计算线数时画了一个空白.

I don't know however how to count 'spaces' in the file. My character counter counts only letters, spaces are excluded.
Plus I'm drawing a blank when it comes to counting lines.

推荐答案

您可以使用 len(data) 作为字符长度.

You can just use len(data) for the character length.

您可以使用 按行拆分 data.splitlines() 方法,结果的长度就是行数.

You can split data by lines using the .splitlines() method, and length of that result is the number of lines.

但是,更好的方法是逐行读取文件:

But, a better approach would be to read the file line by line:

chars = words = lines = 0
with open(file_name, 'r') as in_file:
    for line in in_file:
        lines += 1
        words += len(line.split())
        chars += len(line)

现在即使文件很大,程序也能运行;它在内存中一次不会保存多于一行(加上 python 保留的一个小缓冲区,以使 in_file 中的 for 行: 循环更快一点).

Now the program will work even if the file is very large; it won't hold more than one line at a time in memory (plus a small buffer that python keeps to make the for line in in_file: loop a little faster).

这篇关于计算文件中的字符和行数 python 2.7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆