学习python的第三天,,用python实现词


用python实现词频统计时比较简单,但是需要区分是英文文本还是中文文本,两种不同的文本用到的方法稍微有点区别。

对英文文本进行统计:

def getText():    txt = open("word.txt", "r").read()    txt = txt.lower()    for ch in ‘`~!"@$%^&*()_+-=|\:";<>?,"./‘:        txt = txt.replace(ch, " ")    return txttxt = getText()words = txt.split()counts = {}for word in words:    counts[word] = counts.get(word, 0) + 1for word in counts:    print(word + " " + str(counts.get(word)))

对中文文本进行统计:

import jiebatxt = open("word.txt", "r", encoding="utf-8").read()words = jieba.lcut(txt)counts = {}for word in words:    counts[word] = counts.get(word, 0) + 1for word in counts:    print(word + " " + str(counts[word]))

文件操作:

(1)文件的打开:

rt = open("word.txt", "r")
‘r‘   只读模式,默认值,如果文件不存在,返回FileNotFoundError‘w‘   覆盖写模式,文件不存在则创建,存在则完全覆盖‘x‘   创建写模式,文件不存在则创建,存在则返回FileExistsError‘a‘   追加写模式,文件不存在则创建,存在则在文件最后追加内容‘b‘   二进制文件模式‘t‘   文本文件模式,默认值‘+‘   与r/w/x/a一同使用,在原功能基础上增加同时读写功能

(2)文件的关闭:

rt.close()

学习python的第三天

评论关闭