专题网站开发报价,怎么做一直弹窗口网站,企业推广图片,自学制作app需要多久原始数据和处理结果#xff1a;
https://gitcode.net/as604049322/blog_data/-/tree/master/mdx
下载help.mdx词典后#xff0c;我们无法直接查看#xff0c;我们可以使用readmdict库来完成对mdx文件的读取。
安装库#xff1a;
pip install readmdict对于Windows平台还…原始数据和处理结果
https://gitcode.net/as604049322/blog_data/-/tree/master/mdx
下载help.mdx词典后我们无法直接查看我们可以使用readmdict库来完成对mdx文件的读取。
安装库
pip install readmdict对于Windows平台还需要安装python-lzo
pip install python-lzo使用Python读取的示例
from readmdict import MDXmdx_file help.mdx
mdx MDX(mdx_file, encodingutf-8)
items mdx.items()
for key, value in items:word key.decode().strip()print(word, value.decode())breaka link typetext/css relstylesheet hrefjsmind.cssscript typetext/javascript srcjsmind.js/scriptp idjsmind_describe/pp idjsmind_container/pscriptjsMind.show({},{meta:{name:etymology,version:0.1},format:node_array,data:[{id:a,isroot:true,topic:a,describe:英[ə; eɪ]美[ə; e]art. 一}]});document.getElementById(jsmind_container).style.heightdocument.querySelector(jmnodes).style.height;/script可以看到词典详情数据以JavaScript脚本形式存在我们可以使用正则json进行解析
import rejson.loads(re.findall(data:(\[.\])}\);, value.decode())[0])[{id: a,isroot: True,topic: a,describe: 英[ə; eɪ]美[ə; e]art. 一}]当然这只是最简单的一种情况下面我们看看一个存在树形关系的单词的例子 from readmdict import MDX
import remdx_file help.mdx
mdx MDX(mdx_file, encodingutf-8)
items mdx.items()
for key, value in items:word key.decode().strip()topic json.loads(re.findall(data:(\[.\])}\);, value.decode())[0])if word abalienate:print(word, topic)breakabalienate [{id: abalienate, isroot: True, topic: abalienate, describe: 英[æbeiljəneit]美[æbeiljəneit]【法】 让渡, 转移, 让出}, {id: ab-, parentid: abalienate, direction: left, topic: ab-, describe: 表示从来自from从...离开离开away from, sway, off不非表否定not, opposite。在字母v 前缩略成a-在字母c, t 前扩展为abs-。来自拉丁介词ab。}, {id: alienate, parentid: abalienate, direction: left, topic: alienate, describe: 英[eɪlɪəneɪt]vt. 使疏远, 离间, 转让\n【第三人称单数alienates现在分词alienating过去式alienated】}, {id: alien, parentid: alienate, direction: left, topic: alien, describe: 英[eɪlɪən]美[ˈeliən,ˈeljən]n. 外国人, 外侨\na. 外国的, 相异的\n【复数aliens现在分词aliening过去分词aliened】}, {id: -ate, parentid: alienate, direction: left, topic: -ate, describe: [[表动词“做造成”。]]}, {id: ali-, parentid: alien, direction: left, topic: ali-, describe: [[ other, to change, 表示“其他的改变状态”来源于拉丁语 alius another, other, different.]]}, {id: -en, parentid: alien, direction: left, topic: -en, describe: [[表名词“人或物”有时构成小词或昵称。]]}]同时我们可以看到有部分词的描述可能会嵌套列表。
下面我们的目标是将每个单词都处理成如下形式 最终的完整代码为
from readmdict import MDX
import re
import json
import csvdef get_describe(describe):if isinstance(describe, (list, tuple)):return ;.join(get_describe(i) for i in describe)else:return describedef deal_node(node, result[], num-1):chars ■□◆▲●◇△○★☆for k, (d, cs) in node.items():if num 0:d d.replace(\n, )result.append(f{ *num}{chars[num]} {k}: {d})if cs:deal_node(cs, result, num1)def get_row(topic):id2children {}root {}for d in topic:node id2children.get(d.get(parentid), root)tmp {}node[d[id]] (get_describe(d[describe]), tmp)id2children[d[id]] tmpname, (describe, _) list(root.items())[0]txts []deal_node(root, txts)other \n.join(txts)return name, describe, othermdx_file help.mdx
mdx MDX(mdx_file, encodingutf-8)
items mdx.items()
data []
for key, value in items:word key.decode().strip()topic json.loads(re.findall(data:(\[.\])}\);, value.decode())[0])name, describe, other get_row(topic)data.append((name, describe, other))with open(mdx_file.replace(.mdx, -UTF8 .csv), w, newline, encodingu8) as f:cw csv.writer(f, delimiter,)cw.writerow([单词, 释义, 扩展])cw.writerows(data)