1. 在加载dict_file文件时使用的是
def __init__(self, dict_file='word.data'): self.dict_file = dict_file根本没有考虑程序文件是基于框架并不是在当前程序目录中运行,因此会导致找不到dict_file文件而报错。更好的方法应该是基于当前路径去加载文件。
def __init__(self, dict_file='word.data'): abs_path = os.path.dirname(__file__) self.dict_file = abs_path + '/' + dict_file2. 在读取dict_file文件时报错
with file(self.dict_file) as f_obj:
NameError: name 'file' is not defined, 因python版本升级,函数使用有所变化,需要把file函数改成open函数从而解决问题。
3. 在字符进行解码的时候unicode要改成str
if not isinstance(string, unicode):
string = string.decode("utf-8")
会报错:NameError: name 'unicode' is not defined。 Python2 的unicode 函数在 Python3 中被命名为 str。在 Python3 中使用 ·str 来代替 Python2 中的 unicode即可解决问题。
本文地址:http://api.04007.cn/article/1214.html 未经许可,不得转载. 手机访问本页请扫描右下方二维码.
![]() |
![]() |
手机扫码直接打开本页面 |