python学习随笔01 分析多个编码方式不同的文本,将文件名和编码方式存在字典中

# 分析多个文本
import  chardet
filenames = ['file\pi_digits.txt', 'file\guest.txt', 'file\words.txt']
encod_file = dict()
def get_charset(filename):
    with open(filename, 'rb') as f:
        t = f.read()
    encod_file[filename] = chardet.detect(t)['encoding']
    f.close()
for i in filenames:
    get_charset(i)

def count_words(filename,encoding):
    try:
        with open(filename,encoding=encoding) as f:
            t = f.read()
    except FileNotFoundError:
        print("%s not found" % filename)
    else:
        words = t.split()
        num_words = len(words)
        print("%s words found in %s" % (num_words,filename))

for k,v in encod_file.items():
    count_words(k,v)
posted @ 2024-01-17 16:29  大龙挂了,公主在哪  阅读(13)  评论(0)    收藏  举报