# -*- coding: utf-8 -*- """ Created on Thu Aug 25 10:06:52 2016 审核性味归经与功能的脚本 @author: Administrator """ import xlrd,csv import list_xwgj_function,chi_square,list_xwgj_function_multiElements #三元素组合[寒,苦,肝] thelist_xwgj_function=list_xwgj_function.thelist_xwgj_function #多元素组合 #thelist_xwgj_function=list_xwgj_function_multiElements.list_xwgj2_function fileName="性味归经_功能_关系.csv" excelFilename="中药表.xlsx" sheetName="Sheet1" #打开excel数据 excelFile=xlrd.open_workbook(excelFilename) sheet=excelFile.sheet_by_name(sheetName) #表单行与列 number_rows=sheet.nrows number_columns=sheet.ncols #表单内数据 list_sheetData=[] # list_xingWeiFunction_relation=[] #获取表格数据 def Get_sheetData(): for row in range(1,number_rows): list_sheetData.append(sheet.row_values(row)) return list_sheetData list_sheetData=Get_sheetData() #卡方统计的keyWord1与keyWord2的关系 def Get_single_chiSquare(keyWord1,keyWord2): list_a=[] a=0 b=0 c=0 d=0 #统计包含a数量 for k in range(len(list_sheetData)): i=list_sheetData[k] if keyWord1[0] in i[0] and keyWord1[1] in i[0] and keyWord1[2] in i[0] and keyWord2 in i[1]: a+=1 list_a.append(k+2) #统计包含b数量 for i in list_sheetData: if keyWord1[0] in i[0] and keyWord1[1] in i[0] and keyWord1[2] in i[0] and keyWord2 not in i[1]: b+=1 #统计包含c数量 for i in list_sheetData: if keyWord1[0] not in i[0] or keyWord1[1] not in i[0] or keyWord1[2] not in i[0] and keyWord2 in i[1]: c+=1 #统计包含d数量 for i in list_sheetData: if keyWord1[0] not in i[0] or keyWord1[1] not in i[0] or keyWord1[2] not in i[0] and keyWord2 not in i[1]: d+=1 #print("a:",a) #print("b:",b) #print("c:",c) #print("d:",d) k2=chi_square.value_independence(a,b,c,d) relation=chi_square.judge_independence(a,b,c,d) lis1=[[a,b,c,d],k2,relation,list_a] return lis1 #计算所有性味的相关性 def Get_all_chiSquare(thelist_xwgj_function): for i in thelist_xwgj_function: try: keyWord1,keyWord2=i[0],i[1] relation=Get_single_chiSquare(keyWord1,keyWord2) list1=[i,relation,relation[2]] list_xingWeiFunction_relation.append(list1) except: print("wrong at:",i) continue return list_xingWeiFunction_relation def Write_table_to_csv(list1,fileName): #对列表格式修改,字符串写入的格式不对 file=open(fileName,‘w‘,newline=‘‘) writer1=csv.writer(file) writer1.writerows(list1) file.close() list_xingWeiFunction_relation=Get_all_chiSquare(thelist_xwgj_function) Write_table_to_csv(list_xingWeiFunction_relation,fileName)
需要导入中药表
时间: 2024-11-05 21:43:08