20211116
This commit is contained in:
1 parent
cc5ad90fd1
commit
1b88f510c6
2 files changed
+208
-1
No files matched your search
+206
@@ -0,0 +1,206 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## 正则表达式分割文本"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import json\n",
|
||||
"import openpyxl\n",
|
||||
"import re\n",
|
||||
"\n",
|
||||
"file_name = 'data/药食材性味归经.xlsx'\n",
|
||||
"wb = openpyxl.load_workbook(file_name)\n",
|
||||
"sheet = wb.active\n",
|
||||
"#sheets = wb.sheetnames\n",
|
||||
"\n",
|
||||
"list1 = []\n",
|
||||
"dict1 = {}\n",
|
||||
"mo =r'[。入归].+经$'\n",
|
||||
"mo1 = r'[二]'\n",
|
||||
"mo2 = r'[,、;]'\n",
|
||||
"for n in range(2,sheet.max_row):\n",
|
||||
" name = sheet.cell(n,1).value\n",
|
||||
" content = re.findall(mo,sheet.cell(n,2).value)\n",
|
||||
" if len(content) > 0:\n",
|
||||
" l = len(content[0])\n",
|
||||
" gj = re.sub(mo1, '', content[0][1:l-1]) \n",
|
||||
" list_gj = re.split(mo2,gj)\n",
|
||||
" dict1[sheet.cell(n,1).value ] = list_gj\n",
|
||||
" #dict1['guijing'] = list_gj\n",
|
||||
" \n",
|
||||
"filename = './data/药食材归经.json'\n",
|
||||
"with open(filename,'w') as fl:\n",
|
||||
" json.dump(dict1, fl,ensure_ascii=False) \n",
|
||||
"\n",
|
||||
"#print(list1)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## 药膳归经明细文件生成"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import json\n",
|
||||
"\n",
|
||||
"filename = 'data/药膳210927.json'\n",
|
||||
"with open(filename,'r') as fl:\n",
|
||||
" dict1 = json.load(fl)\n",
|
||||
"filename = 'data/药食材归经.json'\n",
|
||||
"with open(filename,'r') as fl:\n",
|
||||
" dict2 = json.load(fl)\n",
|
||||
"k_zy = dict2.keys()\n",
|
||||
"dict3 = {}\n",
|
||||
"list1 = []\n",
|
||||
"for item in dict1:\n",
|
||||
" print(item['name'])\n",
|
||||
" list1 = []\n",
|
||||
" #print(i,item['zy'])\n",
|
||||
" for m_zy in item['zy']:\n",
|
||||
" if m_zy in k_zy:\n",
|
||||
" dict4 = {}\n",
|
||||
" print(m_zy,dict2[m_zy])\n",
|
||||
" dict4[m_zy] = dict2[m_zy]\n",
|
||||
" list1.append(dict4)\n",
|
||||
" dict3[item['name']] = list1\n",
|
||||
"filename = './data/药膳药食材.json'\n",
|
||||
"with open(filename,'w') as fl:\n",
|
||||
" json.dump(dict3, fl,ensure_ascii=False) \n",
|
||||
" \n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## 药膳归经权重生成"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 60,
|
||||
"metadata": {
|
||||
"execution": {
|
||||
"iopub.execute_input": "2021-11-16T13:34:52.373277Z",
|
||||
"iopub.status.busy": "2021-11-16T13:34:52.372041Z",
|
||||
"iopub.status.idle": "2021-11-16T13:34:52.529747Z",
|
||||
"shell.execute_reply": "2021-11-16T13:34:52.527803Z",
|
||||
"shell.execute_reply.started": "2021-11-16T13:34:52.373167Z"
|
||||
},
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import json\n",
|
||||
"import openpyxl\n",
|
||||
"import os,sys,shutil\n",
|
||||
"\n",
|
||||
"filename = './data/药膳药食材.json'\n",
|
||||
"with open(filename,'r') as fl:\n",
|
||||
" dict1 = json.load(fl)\n",
|
||||
"list1 = []\n",
|
||||
"for k, v in dict1.items():\n",
|
||||
" if len(v) > 0 :\n",
|
||||
" dict2 = {}\n",
|
||||
" #print(k,v)\n",
|
||||
" ys_name = k\n",
|
||||
" dict2.setdefault(ys_name,{})\n",
|
||||
" for item in v:\n",
|
||||
" for k1, v1 in item.items():\n",
|
||||
" for item1 in v1:\n",
|
||||
" dict2[ys_name].setdefault(item1,0)\n",
|
||||
" dict2[ys_name][item1] += 1\n",
|
||||
" list1.append(dict2) \n",
|
||||
" \n",
|
||||
"dict_qz = {}\n",
|
||||
"for item in list1:\n",
|
||||
" for k, v in item.items():\n",
|
||||
" qz = sorted(v.items(), key = lambda kv:(kv[1], kv[0]),reverse=True)\n",
|
||||
" dict_qz[k] = qz\n",
|
||||
"#print(dict_qz)\n",
|
||||
"qz_key = dict_qz.keys()\n",
|
||||
"filename = 'data/药膳210927.json'\n",
|
||||
"with open(filename,'r') as fl:\n",
|
||||
" dict1 = json.load(fl)\n",
|
||||
"#print(dict1)\n",
|
||||
"wb = openpyxl.Workbook()\n",
|
||||
"sheet = wb.active\n",
|
||||
"sheet['A1'] = '药膳名称'\n",
|
||||
"sheet['B1'] = '来源'\n",
|
||||
"sheet['C1'] = '配方'\n",
|
||||
"sheet['D1'] = '做法'\n",
|
||||
"sheet['E1'] = '功效'\n",
|
||||
"sheet['F1'] = '中药成分'\n",
|
||||
"sheet['G1'] = '归经权重'\n",
|
||||
"\n",
|
||||
"i =2\n",
|
||||
"for item in dict1:\n",
|
||||
" sheet[f'A{i}'] = item['name']\n",
|
||||
" sheet[f'B{i}'] = item['source']\n",
|
||||
" sheet[f'C{i}'] = item['pf']\n",
|
||||
" sheet[f'D{i}'] = item['zf']\n",
|
||||
" sheet[f'E{i}'] = item['gx']\n",
|
||||
" sheet[f'F{i}'] = ','.join(item['zy'])\n",
|
||||
" if item['name'] in qz_key:\n",
|
||||
" s = ''\n",
|
||||
" for m_gj in dict_qz[item['name']]:\n",
|
||||
" s = s+ m_gj[0] +'('+str(m_gj[1])+')'\n",
|
||||
" sheet[f'G{i}'] = s\n",
|
||||
" else:\n",
|
||||
" sheet[f'G{i}'] = '暂无归经'\n",
|
||||
" i += 1\n",
|
||||
"\n",
|
||||
"wb.save('data/test4.xlsx') \n",
|
||||
"\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.8.10"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 4
|
||||
}
|
||||
Reference in new issue
Block a user