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设计】。

相关文章

python分布式编程实现过程解析

分布式编程的难点在于: 1.服务器之间的通信,主节点如何了解从节点的执行进度,并在从节点之间进行负载均衡和任务调度; 2.如何让多个服务器上的进程访问同一资源的不同部分进行执行 第一部分...

详解Matplotlib绘图之属性设置

详解Matplotlib绘图之属性设置

关于Python数据分析在数学建模中的更多相关应用:Python数据分析在数学建模中的应用汇总(持续更新中!) (1)、导入库 import matplotlib.pyplot a...

Python Xml文件添加字节属性的方法

实例如下所示: from xml.etree.cElementTree import ElementTree,Element import xlrd import re def re...

Java多线程编程中ThreadLocal类的用法及深入

ThreadLocal,直译为“线程本地”或“本地线程”,如果你真的这么认为,那就错了!其实,它就是一个容器,用于存放线程的局部变量,我认为应该叫做 ThreadLocalVariabl...

python 集合 并集、交集 Series list set 转换的实例

set转成list方法如下: list转成set方法如下: s = set('12342212')       &n...