计算字母在python中显示在文本文件中的次数

时间:2016-04-19 18:37:14

标签: python

我的目标是创建一个python脚本,用于计算每个字母从文本文件中出现的次数。因此,如果文本文件包含Hi there,则输出将类似于

E is shown 2 times
H is shown 2 times
I is shown 1 time
R is shown 1 time
T is shown 1 time

我已经尝试了不同的方法来获取此功能,但由于我继续获取语法错误,因此无法显示输出。我尝试了以下

import collections
import string

def count_letters(example.txt, case_sensitive=False):
    with open(example.txt, 'r') as f:
        original_text = f.read()
    if case_sensitive:
        alphabet = string.ascii_letters
        text = original_text
    else:
        alphabet = string.ascii_lowercase
        text = original_text.lower()
    alphabet_set = set(alphabet)
        counts = collections.Counter(c for c in text if c in alphabet_set)

    for letter in alphabet:

        print(letter, counts[letter])
    print("total:", sum(counts.values()))

    return counts

并且

def count_letters(example.txt, case_sensitive=False):
    alphabet = "abcdefghijlkmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVXYZ"
    with open(example.txt, 'r') as f:
        text = f.read()
    if not case_sensitive:
        alpahbet = alphabet[:26]
        text = text.lower()
        letter_count = {ltr: 0 for ltr in alphabet}
    for char in text:
        if char in alphabet:
            letter_count[char] += 1
    for key in sorted(letter_count):
        print(key, letter_count[key])
    print("total", sum(letter_count())) 

2 个答案:

答案 0 :(得分:1)

运行脚本时发现了一些问题。 @Priyansh Goel在答案中正确找到了一个:你不能使用example.txt作为参数。您应该只选择text_file之类的变量名称,并在调用该函数时传入文件名的字符串。

还有一两个缩进错误。这是我开始工作的剧本:

import collections
import string

def count_letters(text_file, case_sensitive=False):
    with open(text_file, 'r') as f:
        original_text = f.read()
    if case_sensitive:
        alphabet = string.ascii_letters
        text = original_text
    else:
        alphabet = string.ascii_lowercase
        text = original_text.lower()
        alphabet_set = set(alphabet)
        counts = collections.Counter(c for c in text if c in alphabet_set)

    for letter in alphabet:
        print(letter, counts[letter])

    print("total:", sum(counts.values()))

    return counts

count_letters("example.txt")

如果您只在"example.txt"上使用它,只需删除第一个参数并将文件名硬编码到函数中:

def count_letters(case_sensitive=False):
    with open("example.txt", 'r') as f:
        ...

count_letters()

作为程序员,您可以开发的最佳技能之一是学习阅读和理解所引发的错误。它们并不意味着可怕或令人沮丧(虽然有时它们是),但它们本来是有帮助的。像你所拥有的语法错误特别有用。如果错误指示的内容并不完全明显,请将错误复制并粘贴到Google搜索中,通常情况下,您会发现问题的答案已经存在。

学习好运! Python是(大概)第一语言的绝佳选择!

答案 1 :(得分:-1)

在您的功能中,您不能将example.txt作为参数名称。

以下代码仅遍历文本字母而不是整个字母集。我用dict来存储字母的频率。使用isalpha以便我们只将字母放入字典中。

import collections
import string

def count_letters(textfile, case_sensitive=False):
    with open(textfile, 'r') as f:
        original_text = f.read()
        if case_sensitive:

            text = original_text
        else:

            text = original_text.lower()

        p = dict()

        for i in text:
            if i in p.keys():
                p[i] += 1
            elif i.isalpha():
                p[i] = 1;

        keys = p.keys()

        for k in keys:
            print str(k) + " " + str(p[k])


count_letters("example.txt")