python:mdict + bottle = web 查询英汉词典

您所在的位置:网站首页 mdict电脑版下载 python:mdict + bottle = web 查询英汉词典

python:mdict + bottle = web 查询英汉词典

#python:mdict + bottle = web 查询英汉词典| 来源: 网络整理| 查看: 265

用chrome 访问 https://www.lfd.uci.edu/~gohlke/pythonlibs/#python-lzo 下载 python_lzo-1.12-cp37-cp37m-win_amd64.whl pip install /pypi/python_lzo-1.12-cp37-cp37m-win_amd64.whl

pip install readmdict ; 参见:使用Python调用mdx字典文件进行查词

安装 Mdict 去 MDict pip install bottle

mdict_bottle.py

# -*- coding: utf-8 -*- """ web server 用于查询英汉词典 """ import os import sys import json import time from readmdict import MDX from bottle import route, run, post, request, static_file os.chdir("/mdict") start_time = time.time() # 加载.mdx文件 filename = "your.mdx" mdx = MDX(filename) headwords = [*mdx] # 单词名列表 items = [*mdx.items()] # 释义html源码列表 n = len(headwords) m = len(items) if n == m: print(f'{filename} 加载成功:共{n}条') end_time = time.time() print('cost %f second' % (end_time - start_time)) else: print(f'ERROR:加载失败 {n}!={m}') sys.exit(1) @route('/') def server_static(filepath="index.html"): return static_file(filepath, root='./') # 静态资源的目录通常称为public或static @route('/') def server_static(filepath): return static_file(filepath, root='./') def eng_han(txt): """ 英译中 """ if not txt.isascii(): return 'Maybe text is not english' word = txt.encode() word1 = txt.capitalize().encode() # 第1个字母变大写 global headwords, items try: # 查词,返回单词和html文件 if word in headwords: wordIndex = headwords.index(word) else: wordIndex = headwords.index(word1) word,html = items[wordIndex] result = html.decode() result = result.replace('


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3