def main(): py_location = os.path.dirname(os.path.abspath(__file__)) print(py_location) # 打印当前 py 文件所在目录 workbook = xlrd.open_workbook(os.path.join(py_location, r"通用规范汉字表.xls")) dictionary = {} for sheet in workbook.sheets(): for row in range(sheet.nrows): zi_tou = sheet.cell(row, 1).value xu_hao = str(int(sheet.cell(row, 0).value)).zfill(4) print(sheet.name, xu_hao, zi_tou) #f.write(zi_tou + '\t' + xu_hao + '\t' + sheet.name + '\n') dictionary[ zi_tou] = '<b>' + zi_tou + '</b>' + '\t' + xu_hao + '\t' + sheet.name writer = MDictWriter(dictionary, title="", description="") outfile = open(os.path.join(py_location, r"通用规范汉字表.mdx"), "wb") writer.write(outfile) outfile.close() print('done!')
def write_mdict(): """ Zip/Write button's command: zip/write .txt files into a file that ends with "mdx"/"mdd". (Adapted from "readmdict", cf. https://jingyan.baidu.com/article/95c9d20d47583bec4e756132.html) Returns: If successful, internal files of .mdx/.mdd will appear in a folder (named ""Unzipped_mdx) in the same folder as .mdx/.mdd, and an messagebox will pop up showing the number of entries in .mdx or the number of files in .mdd. If failed, an errorbox will pop up requesting you to specify a valid MDX/MDD file. """ import io import sys import importlib importlib.reload(sys) import collections from collections import defaultdict from writemdict import MDictWriter, encrypt_key from ripemd128 import ripemd128 # Modified by 掌上百科@maomaowei5655 in 2020-04-23: if write_path.split(os.path.sep)[-1].endswith('mdx'): # Zip files (dictionary_name.txt (and about.txt)) into .mdx # Delete .DS_Store in Mac: files_in_write_path = os.listdir(write_path) if '.DS_Store' in files_in_write_path: files_in_write_path.remove('.DS_Store') # The folder can only have at most 2 files: if len(files_in_write_path) > 2: tkinter.messagebox.showerror( 'Error', "The number of files in the folder must be <=2.") elif len(files_in_write_path) == 0: tkinter.messagebox.showerror( 'Error', "The number of files in the folder must be <=2.") elif len(files_in_write_path) == 1: # When the folder only has one .txt including entries global dictionary_txt dictionary_txt = files_in_write_path[0] head = 0 new_mean = [] f = io.open(os.path.join(write_path, dictionary_txt), 'r', encoding='utf-8') d = defaultdict(list) # 建立一个空字典,也可使用{}建立。 for line in f: # 每次从f中读入一行 line = line.rstrip('\n') # 去除行尾的换行符 if line == '</>': if head == 2: new_mean[0:] = ["".join(new_mean[0:])] d[word].append(new_mean[0]) head = 1 new_mean = [] elif head == 1: word = line head = 2 elif head == 2: new_mean.append(line) head = 2 f.close() # Modified by 掌上百科@maomaowei5655 in 2020-04-23: if not os.path.exists(os.path.join(write_path, 'Zipped_mdx')): os.makedirs(os.path.join(write_path, 'Zipped_mdx')) outfile_mdx = open( os.path.join(write_path, 'Zipped_mdx', dictionary_txt.split(os.path.extsep)[0] + '.mdx'), "wb") writer = MDictWriter(d, dictionary_txt.split(os.path.extsep)[0], dictionary_txt.split(os.path.extsep)[0]) writer.write(outfile_mdx) outfile_mdx.close() # Modified by 掌上百科@maomaowei5655 in 2020-04-23: print('======== {} ========'.format(write_path)) global write_number_report write_number_report = '{}.mdx'.format( dictionary_txt.split(os.path.extsep)[0]) print('Output: {}'.format(write_number_report)) # Modified by 掌上百科@maomaowei5655 in 2020-04-23: # region Messagebox for reporting outcome: tkinter.messagebox.showinfo( 'Output', 'Write/Zip: \n\t{}\n\nOutput: \n\t{}'.format( write_path, write_number_report)) # endregion else: # When the folder have two .txt files including entries and about_information for file in files_in_write_path: if not file.startswith('about'): dictionary_txt = file else: global about_txt about_txt = file head = 0 new_mean = [] f = io.open(os.path.join(write_path, dictionary_txt), 'r', encoding='utf-8') d = defaultdict(list) # 建立一个空字典,也可使用{}建立。 for line in f: # 每次从f中读入一行 line = line.rstrip('\n') # 去除行尾的换行符 if line == '</>': if head == 2: new_mean[0:] = ["".join(new_mean[0:])] d[word].append(new_mean[0]) head = 1 new_mean = [] elif head == 1: word = line head = 2 elif head == 2: new_mean.append(line) head = 2 f.close() ff = io.open(os.path.join(write_path, about_txt), 'r', encoding='utf-8') # 词典about信息,txt文件请保存为utf-8 about = [] for line in ff: # 每次从f中读入一行 about.append(line) about[0:] = ["".join(about[0:])] # Modified by 掌上百科@maomaowei5655 in 2020-04-23: if not os.path.exists(os.path.join(write_path, 'Zipped_mdx')): os.makedirs(os.path.join(write_path, 'Zipped_mdx')) outfile_mdx = open( os.path.join(write_path, 'Zipped_mdx', dictionary_txt.split(os.path.extsep)[0] + '.mdx'), "wb") writer = MDictWriter(d, dictionary_txt.split(os.path.extsep)[0], about[0]) writer.write(outfile_mdx) outfile_mdx.close() # Modified by 掌上百科@maomaowei5655 in 2020-04-23: print('======== {} ========'.format(write_path)) write_number_report = '{}.mdx'.format( dictionary_txt.split(os.path.extsep)[0]) print('Output: {}'.format(write_number_report)) # Modified by 掌上百科@maomaowei5655 in 2020-04-23: # region Messagebox for reporting outcome: tkinter.messagebox.showinfo( 'Output', 'Write/Zip: \n\t{}\n\nOutput: \n\t{}'.format( write_path, write_number_report)) # endregion # elif write_path.split(os.path.sep)[-1].endswith('mdd'): # # Zip files in mdd # folder_name = re.search(r'^.*{}'.format(os.path.sep), write_path).group() + 'Zipped_mdd' # if not os.path.exists(folder_name): # os.makedirs(folder_name) # outfile_name = os.path.join(folder_name, write_path.split(os.path.sep)[-1]) # for file in os.listdir(write_path): # if file.lower().endswith('png'or'gif'or'jpg'or'jpeg'or'css'): # file_object = open(os.path.join(write_path, file), 'rb') # file_contents = file_object.read() # outfile_mdd = open(outfile_name, 'wb') # d = {"\\{}".format(file) : file_contents} # writer = MDictWriter(d, file.split('.')[0], file, is_mdd=True) # writer.write(outfile_mdd) # file_object.close() # outfile_mdd.close() # else: # file_object = open(os.path.join(write_path, file), 'r', encoding='utf-8', errors='ignore') # file_contents = file_object.read() # outfile_mdd = open(outfile_name, 'w') # d = {"\\{}".format(file): file_contents} # writer = MDictWriter(d, file.split('.')[0], file, is_mdd=True) # writer.write(outfile_mdd) # file_object.close() # outfile_mdd.close() # write_number_report = outfile_name # print('Output: {}'.format(write_number_report)) # # region Messagebox for reporting outcome: # tkinter.messagebox.showinfo('Output', 'Write/Zip: \n\t{}\n\nOutput: \n\t{}'.format(write_path, write_number_report)) # # endregion else: # Modified by 掌上百科@maomaowei5655 in 2020-04-23: print('Please put files in a folder suffixed mdx/mdd.') # region Messagebox for error: tkinter.messagebox.showerror('Error', "Please specify a valid MDX file.")
# directory. from writemdict import MDictWriter, encrypt_key from ripemd128 import ripemd128 import io # This is the dictionary we will use. d = { "alpha":"<i>alpha</i>", "beta":"Letter <b>beta</b>", "gamma":"Capital version is Γ <"} ### Example 1: Basic writing. All options default. outfile = open("example_output/basic.mdx", "wb") writer = MDictWriter(d, "Basic dictionary", "This is a basic test dictionary.") writer.write(outfile) outfile.close() ### Example 2: Demonstrates the use of UTF-16 encoding. outfile = open("example_output/utf16.mdx", "wb") writer = MDictWriter(d, "UTF-16 dictionary", "This is a test for the \"UTF-16\" encoding.", encoding="utf-16") writer.write(outfile) outfile.close() ### Example 3: This is a test to create a UTF-16 dictionary containing characters outside the # Basic Multilingual Plane
# directory. from writemdict import MDictWriter, encrypt_key from ripemd128 import ripemd128 import io # This is the dictionary we will use. d = { "alpha": "<i>alpha</i>", "beta": "Letter <b>beta</b>", "gamma": "Capital version is Γ <" } ### Example 1: Basic writing. All options default. outfile = open("example_output/basic.mdx", "wb") writer = MDictWriter(d, "Basic dictionary", "This is a basic test dictionary.") writer.write(outfile) outfile.close() ### Example 2: Demonstrates the use of UTF-16 encoding. outfile = open("example_output/utf16.mdx", "wb") writer = MDictWriter(d, "UTF-16 dictionary", "This is a test for the \"UTF-16\" encoding.", encoding="utf-16") writer.write(outfile) outfile.close() ### Example 3: This is a test to create a UTF-16 dictionary containing characters outside the # Basic Multilingual Plane d2 = {"𩷶": "A fish"}
#+'<button type="button" onclick="startmy()">Click Me!</button>' +'<canvas id="glcanvas" width="640" height="480" >' +' asdasd' +'</canvas>' +'</div>' +'<div id="the-div">Content</div>' +'<script>$(document).trigger("startmyfunc");</script>') # This is the dictionary we will use. d = { "alpha":asd, "beta":"Letter <b>beta</b>", "gamma":"Capital version is Γ <"} ### Example 4: Uses the Big5 encoding. outfile = open("example_output/big5.mdx", "wb") writer = MDictWriter(d, "Big5 dictionary", "This is a test for the \"Big5\" encoding.", encoding="big5") writer.write(outfile) outfile.close()
from ripemd128 import ripemd128 import io # This is the dictionary we will use. d = { "alpha":"<i>alpha</i>", "beta":"Letter <b>beta</b>", "gamma":"Capital version is Γ <"} ### Example 1: Basic writing. All options default. outfile = open("example_output/basic.mdx", "wb") writer = MDictWriter(d, "Basic dictionary", "This is a basic test dictionary.") writer.write(outfile) outfile.close() ### Example 2: Demonstrates the use of UTF-16 encoding. outfile = open("example_output/utf16.mdx", "wb") writer = MDictWriter(d, "UTF-16 dictionary", "This is a test for the \"UTF-16\" encoding.", encoding="utf-16") writer.write(outfile) outfile.close() ### Example 3: This is a test to create a UTF-16 dictionary containing characters outside the # Basic Multilingual Plane
# -*- coding: utf-8 -*- """ ejdicをMDict形式に変換します。 """ from writemdict import MDictWriter, encrypt_key import sys args = sys.argv d = {} with open(args[1]) as f: line = f.readline() while line: word, meaning = line.split('\t', 1) meaning = meaning.replace('/', '\n').replace('・', ',') line = f.readline() d[word] = meaning with open("ejdic.mdx", "wb") as outfile: writer = MDictWriter(d, "EJDIC", "\"UTF-8\" encoding.", encoding="utf-8") writer.write(outfile)
from __future__ import unicode_literals from writemdict import MDictWriter import os import codecs import json filelist = os.listdir() mdxcontent = [] content = [] dictionary = {} for filename in filelist: if (filename.split(".")[-1] not in ["html", "htm"]): continue entryname = filename.replace(".html", '') #".".join(filename.split(".")[0:-1]) with codecs.open(filename, "r+", encoding='utf-8') as ent: entrycontent = ent.read() entry = entryname + "\n" + entrycontent entry = entry + "\n</>" dictionary[entryname] = entrycontent mdxcontent.append(entry) print(filename) with codecs.open("notes.txt", 'w+', encoding='utf-8') as f: f.write("\n".join(mdxcontent).replace("\n\n", "\n").replace("\n\n", "\n")) writer = MDictWriter(dictionary, title="Notes", description="My best notes!") outfile = open("notes.mdx", "wb") writer.write(outfile) outfile.close()