计数器txt文件中的字符总数并给出百分比

时间:2018-10-27 21:19:15

标签: python-3.x

我想知道如何在txt文件中查找“ Toal”字符总数,然后使用该数字将其分成txt文件中每个字母的出现次数。我在开头,结尾和总数中都有最常用的字母的编号。任何帮助表示赞赏。谢谢。

import collections

def go (filename):
    x=collections.Counter()
    with open (filename, encoding= "latin-1") as f:
        for line in f:
            for word in line.split():
                word=word.lower().strip("1234567890[].,?!'][\}{//%^&#@$%^&*()_-|~`,")
                if word:
                    x[word[0]]+=1
    return x.most_common()


def lastletter (filename):
    x=collections.Counter()
    with open (filename, encoding= "latin-1") as f:
        for line in f:
            for word in line.split():
                word=word.lower().strip("1234567890[].,?!'][\}{//%^&#@$%^&*()_-|~`,")
                if word:
                    x[word[-1]]+=1
    return x.most_common()

def all (filename):
    x=collections.Counter()
    with open (filename, encoding= "latin-1") as f:
        for line in f:
            for word in line.split():
                word=word.lower().strip("1234567890[].,?!'][\}{//%^&#@$%^&*()_-|~`,")
                for character in word:
                    x[character]+=1
    return x.most_common()

0 个答案:

没有答案