在python中打印文件中每个字符的统计信息

时间:2018-07-13 17:18:07

标签: python python-3.x

我想做的是获取文件的数据并打印出文件中每个字符的百分比,但是我不想使用重复项。我只需要打印一个具有相关百分比的字符。下面是代码段。

for all_char in text:
    char_counter = 0 
    if count_char(text, all_char) > 1:
        perc1 = 100 / len(text) * count_char(text, all_char)
        print("{0} - {1}%".format(all_char, round(perc1, 2)))
        with open(filename, "w") as w:        #<-------- I need a code to remove a single character
            w.truncate(char_counter)
            char_counter += 1

    elif count_char(text, all_char) == 1:
        perc2 = 100 * count_char(text, all_char) / len(text)
        print("{0} - {1}%".format(all_char, round(perc2, 2)))
        char_counter += 1

上面我创建了一个名为char_counter的变量,该变量将在每次迭代后增加,而名为count_char的函数将告诉文件中每个字符使用了多少次,以及该数字是否大于1。该字符必须从文件中删除,这意味着它将仅打印一次。这是基本思想,但是代码给了我一个错误。

1 个答案:

答案 0 :(得分:2)

通过在字符上使用toml::Value,可以获取整个文件的字符数。那么每个字符的百分比为Counter

count for that character/total count

文本

from collections import Counter
from itertools import chain

with open(filename) as f:
    counts = Counter(chain.from_iterable(f))

total = sum(counts.values())

for character, count in counts.items():
    print('{:<2} - {:>6.2f}%'.format(repr(character)[1:-1], (count/total) * 100))

此打印

Mary had a little lamb.