使用Python查找文件中的字符数 [英] Find the number of characters in a file using Python

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

问题描述

这里是问题:

我有一个文件,上面写着:

I have a file with these words:

hey how are you
I am fine and you
Yes I am fine

然后要求查找单词,行和字符的数量。

And it is asked to find the number of words, lines and characters.

下面是我的程序,但是没有空格的字符数不正确。

Below is my program, but the number of counts for the characters without space is not correct.

字数正确,行数正确。
同一循环中有什么错误?

The number of words is correct and the number of line is correct. What is the mistake in the same loop?

fname = input("Enter the name of the file:")
infile = open(fname, 'r')
lines = 0
words = 0
characters = 0
for line in infile:
    wordslist = line.split()
    lines = lines + 1
    words = words + len(wordslist)
    characters = characters + len(line)
print(lines)
print(words)
print(characters)

输出为:

lines=3(Correct)
words=13(correct)
characters=47

我在网站上看到了多个答案,我感到困惑,因为我没有学习Python的其他功能。

I've looked on the site with multiple answers and I am confused because I didn't learn some other functions in Python. How do I correct the code as simple and basic as it is in the loop I've done?

而没有空格的字符数是35,而带有空格的字符数是45
如果可能的话,我想找到没有空格的字符数。即使有人知道循环可以带空格的字符数也可以。

Whereas the number of characters without space is 35 and with space is 45. If possible, I want to find the number of characters without space. Even if someone know the loop for the number of characters with space that's fine.

推荐答案

对a中所有单词的长度求和行:

Sum up the length of all words in a line:

characters += sum(len(word) for word in wordslist)

整个程序:

with open('my_words.txt') as infile:
    lines=0
    words=0
    characters=0
    for line in infile:
        wordslist=line.split()
        lines=lines+1
        words=words+len(wordslist)
        characters += sum(len(word) for word in wordslist)
print(lines)
print(words)
print(characters)

输出:

3
13
35

此:

(len(word) for word in wordslist)

发电机exp恢复。从本质上讲,它是一行中的循环,可产生每个单词的长度。我们将这些长度直接输入到 sum

is a generator expression. It is essentially a loop in one line that produces the length of each word. We feed these lengths directly to sum:

sum(len(word) for word in wordslist)



改进版本



此版本利用了 枚举 ,这样就节省了两行代码,同时保持了可读性:

Improved version

This version takes advantage of enumerate, so you save two lines of code, while keeping the readability:

with open('my_words.txt') as infile:
    words = 0
    characters = 0
    for lineno, line in enumerate(infile, 1):
        wordslist = line.split()
        words += len(wordslist)
        characters += sum(len(word) for word in wordslist)

print(lineno)
print(words)
print(characters)

此行:

with open('my_words.txt') as infile:

打开文件并承诺在您离开缩进后立即将其关闭。
使用完文件后总是关闭文件。

opens the file with the promise to close it as soon as you leave indentation. It is always good practice to close file after your are done using it.

这篇关于使用Python查找文件中的字符数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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