python加载自定义词典实例

yipeiwu_com6年前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

以上这篇python加载自定义词典实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Django项目中使用JWT的实现代码

Django项目中使用JWT的实现代码

1.requiremwnts: Django版本:2.2 python版本:3.6 djangorestframework版本:3.1 djangorestframew...

Python基础之循环语句用法示例【for、while循环】

本文实例讲述了Python基础之循环语句用法。分享给大家供大家参考,具体如下: while 循环 Python中while语句的一般形式: while 判断条件:  &nbs...

python删除特定文件的方法

本文实例讲述了python删除特定文件的方法。分享给大家供大家参考。具体如下: #!/usr/bin/python # -*- coding: utf-8 -*- import os...

Python 限制线程的最大数量的方法(Semaphore)

如下所示: import threading import time sem=threading.Semaphore(4) #限制线程的最大数量为4个 def gothrea...

PYQT5实现控制台显示功能的方法

PYQT5实现控制台显示功能的方法

界面文件 Ui_ControlBoard.py # -*- coding: utf-8 -*- # Form implementation generated from read...