python加载自定义词典实例

今天小编就为大家分享一篇python加载自定义词典实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

如下所示:

#加载词典 def load_dict_from_file(filepath): _dict = {} try: with io.open(filepath, 'r',encoding='utf-8') as dict_file: for line in dict_file: (key, value) = line.strip().split(' ') #将原本用空格分开的键和值用冒号分开来,存放在字典中 _dict[key] = value except IOError as ioerr: print("文件 %s 不存在" % (filepath)) return _dict

#加载停用词词典 def stopwordslist(filepath): stopwords = {} fstop = io.open(filepath, 'r',encoding='utf-8') for line in fstop: stopwords[line.strip()]=line.strip() fstop.close() return stopwords

相关文章

功能概要:(目前已实现功能)公共展示部分:1.网站首页展示...
大体上把Python中的数据类型分为如下几类: Number(数字) ...
开发之前第一步,就是构造整个的项目结构。这就好比作一幅画...
源码编译方式安装Apache首先下载Apache源码压缩包,地址为ht...
前面说完了此项目的创建及数据模型设计的过程。如果未看过,...
python中常用的写爬虫的库有urllib2、requests,对于大多数比...