fsQCA数据整理自动化

fsQCA数据整理自动化

文章目录

  • fsQCA数据整理自动化
    • 前言
    • 主程序
    • 用法举例
    • 可下载exe文件

前言

本程序致力于fsQCA最后的简单解和中间解画图的自动化,具体用法如下:
(1)在table_head的第一列中写入变量名,第二列中写入在fsQCA中使用对应的变量名
(2)将fsQCA生成的中间解和简单解分别粘贴至intermediate_group_text和parsimonious_group_text中
(3)双击exe文件运行后,整理出的表格将显示在table中

主程序

import xlrd2
import xlwt
from xlutils.copy import copy
from tkinter import messagebox
import tkinter as tk
def createXls(textlist,r_num,c_num):
    workbook = xlwt.Workbook()
    sheet1 = workbook.add_sheet('sheet1', cell_overwrite_ok=True)
    for i in range(1, r_num+1):
        text=textlist[i-1]
        sheet1.write(i,0,text)
    for i in range(1, c_num+1):
        str1='组态'+str(i)
        sheet1.write(0,i,str1)
    workbook.save('./table.xls')

def main():
    root = tk.Tk()
    root.geometry("1000x500")
    root.title("说明")
    L1 = tk.Label(root, text="本程序为fsQCA的数据整理程序,并不提供fsQCA的相关运算")
    L1.pack()
    L2 = tk.Label(root, text="具体用法:")
    L2.pack()
    L3 = tk.Label(root, text="在table_head的第一列中写入变量名,第二列中写入在fsQCA中使用对应的变量名")
    L3.pack()
    L4 = tk.Label(root, text="将fsQCA生成的中间解和简单解分别粘贴至intermediate_group_text和parsimonious_group_text中")
    L4.pack()
    L4 = tk.Label(root, text="双击exe文件运行后,整理出的表格将显示在table中")
    L4.pack()

    fname='table_head.xlsx'
    bk=xlrd2.open_workbook(fname)
    try:
        sh=bk.sheet_by_name('Sheet1')
    except:
        print("error! have not that sheet")
    textlist=[] #原条件变量名
    explain_textlist=[] #fsQCA中所用的条件变量名
    anti_explain_textlist=[] #fsQCA中所用的反向条件变量名
    answer_printlist=[] #图标
    for i in range(0, 4):
        answer_print=sh.cell(i,2).value
        answer_printlist.append(answer_print)
    nrows=sh.nrows
    for i in range(0,nrows):
        text=sh.cell(i,0).value
        explain_text=sh.cell(i,1).value
        anti_explain_text='~'+explain_text
        textlist.append(text)
        explain_textlist.append(explain_text)
        anti_explain_textlist.append(anti_explain_text)

    #ncols=int(input("输入组态数量:"))
    #createXls(textlist,nrows,ncols)


    #中间解处理
    intermediateSaveList = []  # 存档列表
    # 读取文本内容到列表
    with open("intermediate_group_text.txt", "r", encoding='utf-8') as file:
        for line in file:
            line = line.strip('\n')  # 删除换行符
            line = line.split() #分离空格
            intermediateSaveList.append(line)
        file.close()

    intermediateCheckList = []
    for i in range(0,intermediateSaveList.__len__()):   #查看是否存在条件表中不存在的条件变量
        group_text = intermediateSaveList[i][0]
        group_text = group_text.split('*')
        intermediateCheckList.append(group_text)
        flag=0  #标记是否出现条件表中不存在的条件变量
        num = 0  # 条件数
        for j in range(0, intermediateCheckList[i].__len__()):
            for k in range(0, explain_textlist.__len__()):
                if intermediateCheckList[i][j] == explain_textlist[k] or intermediateCheckList[i][j] == anti_explain_textlist[k]:
                    num=num+1
        if num < intermediateCheckList[i].__len__():
            flag=1
            break
    if flag==1:
        messagebox.showinfo(title='注意', message='错误!中间解出现条件表中未出现的条件变量')
        exit(1)

    ncols=intermediateSaveList.__len__()
    createXls(textlist, nrows, ncols)
    intermediateGroupList = []
    for i in range(0,ncols):    #i种组态
        group_text=intermediateSaveList[i][0]
        group_text = group_text.split('*')
        intermediateGroupList.append(group_text)
        group_text_num=intermediateGroupList[i].__len__()
        #print(intermediateGroupList[i])
        for j in range(0,group_text_num):   #组态中有j种条件
            str_group=intermediateGroupList[i][j]
            flag=0  #标记当前条件是否被找到
            for k in range(0,explain_textlist.__len__()):   #条件对比并绘制
                if flag==1:
                    break
                if str_group==explain_textlist[k]:
                    #print("+"+str(k))
                    flag=1
                    fname1 = 'table.xls'  # 相对路径
                    bk1 = xlrd2.open_workbook(fname1)  # 获取当前文档的表
                    try:
                        sh1 = bk1.sheet_by_name('sheet1')
                    except:
                        print("have not that sheet")
                    xlsc = copy(bk1)
                    shtc = xlsc.get_sheet(0)
                    shtc.write(k+1, i + 1, answer_printlist[0])
                    xlsc.save('./table.xls')
            for k in range(0,anti_explain_textlist.__len__()):
                if flag==1 or flag==2:
                    break
                if str_group==anti_explain_textlist[k]:
                    #print("-"+str(k))
                    flag=2
                    fname1 = 'table.xls'  # 相对路径
                    bk1 = xlrd2.open_workbook(fname1)  # 获取当前文档的表
                    try:
                        sh1 = bk1.sheet_by_name('sheet1')
                    except:
                        print("have not that sheet")
                    xlsc = copy(bk1)
                    shtc = xlsc.get_sheet(0)
                    shtc.write(k + 1, i + 1, answer_printlist[2])
                    xlsc.save('./table.xls')
        fname1 = 'table.xls'  # 相对路径
        bk1 = xlrd2.open_workbook(fname1)  # 获取当前文档的表
        try:
            sh1 = bk1.sheet_by_name('sheet1')
        except:
            print("have not that sheet")
        xlsc = copy(bk1)
        shtc = xlsc.get_sheet(0)
        shtc.write(anti_explain_textlist.__len__() + 1, 0, "原始覆盖度")
        shtc.write(anti_explain_textlist.__len__() + 2, 0, "唯一覆盖度")
        shtc.write(anti_explain_textlist.__len__() + 3, 0, "充分一致性")
        shtc.write(anti_explain_textlist.__len__() + 1, i + 1, intermediateSaveList[i][1])
        shtc.write(anti_explain_textlist.__len__() + 2, i + 1, intermediateSaveList[i][2])
        shtc.write(anti_explain_textlist.__len__() + 3, i + 1, intermediateSaveList[i][3])
        xlsc.save('./table.xls')


    # 简单解处理
    parsimoniousSaveList = []  # 存档列表
    # 读取文本内容到列表
    with open("parsimonious_group_text.txt", "r", encoding='utf-8') as file:
        for line in file:
            line = line.strip('\n')  # 删除换行符
            line = line.split()  # 分离空格
            parsimoniousSaveList.append(line)
        file.close()

    parsimoniousCheckList = []
    for i in range(0, parsimoniousSaveList.__len__()):  # 查看是否存在条件表中不存在的条件变量
        group_text = parsimoniousSaveList[i][0]
        group_text = group_text.split('*')
        parsimoniousCheckList.append(group_text)
        flag = 0  # 标记是否出现条件表中不存在的条件变量
        num = 0  # 条件数
        for j in range(0, parsimoniousCheckList[i].__len__()):
            for k in range(0, explain_textlist.__len__()):
                if parsimoniousCheckList[i][j] == explain_textlist[k] or parsimoniousCheckList[i][j] == \
                        anti_explain_textlist[k]:
                    num = num + 1
        if num < parsimoniousCheckList[i].__len__():
            flag = 1
            break
    if flag == 1:
        messagebox.showinfo(title='注意', message='错误!简单解出现条件表中未出现的条件变量')
        exit(1)

    parsimoniousGroupList = []
    for i in range(0, parsimoniousSaveList.__len__()):  # i种组态
        group_text = parsimoniousSaveList[i][0]
        group_text = group_text.split('*')
        parsimoniousGroupList.append(group_text)
        group_text_num = parsimoniousGroupList[i].__len__()
        #print(parsimoniousGroupList[i])
        for j in range(1,ncols+1):  # 对比全部中间解
            flag = 0  # 简单解条件是否全部满足
            for k in range(0, group_text_num):  # 组态中有k种条件
                if flag==1:
                    break
                str_group = parsimoniousGroupList[i][k]
                for m in range(0, explain_textlist.__len__()): # 条件对比并绘制
                    fname1 = 'table.xls'  # 相对路径
                    bk1 = xlrd2.open_workbook(fname1)  # 获取当前文档的表
                    try:
                        sh1 = bk1.sheet_by_name('sheet1')
                    except:
                        print("have not that sheet")
                    if str_group == explain_textlist[m] and sh1.cell_value(m+1, j)!=answer_printlist[0] and sh1.cell_value(m+1, j)!=answer_printlist[1]: #简单解与中间解不匹配
                        flag=1
                        break
                    if str_group == anti_explain_textlist[m] and sh1.cell_value(m+1, j)!=answer_printlist[2] and sh1.cell_value(m+1, j)!=answer_printlist[3]: #简单解与中间解不匹配
                        flag=1
                        break
            if flag==0: #简单解与该中间解相一致
                for k in range(0, group_text_num):
                    str_group = parsimoniousGroupList[i][k]
                    for m in range(0, explain_textlist.__len__()):  # 条件对比并绘制
                        fname1 = 'table.xls'  # 相对路径
                        bk1 = xlrd2.open_workbook(fname1)  # 获取当前文档的表
                        try:
                            sh1 = bk1.sheet_by_name('sheet1')
                        except:
                            print("have not that sheet")
                        xlsc = copy(bk1)
                        shtc = xlsc.get_sheet(0)
                        if str_group == explain_textlist[m]:
                            #print(m+1," ",j)
                            shtc.write(m + 1, j, answer_printlist[1])
                        if str_group == anti_explain_textlist[m]:
                            #print(m + 1, " ", j)
                            shtc.write(m + 1, j, answer_printlist[3])
                        xlsc.save('./table.xls')

    root.mainloop()  # 设置翻页停顿
if __name__=='__main__':
    main()

用法举例

将中间解、简单解分别粘贴至以下txt文件:
fsQCA数据整理自动化_第1张图片
fsQCA数据整理自动化_第2张图片
table_head文件根据自己的变量对第一列和第二列进行修改
fsQCA数据整理自动化_第3张图片
双击exe文件(此时table表格不能打开,否则会报错)
fsQCA数据整理自动化_第4张图片
查看table表格,发现数据已经整理完成
fsQCA数据整理自动化_第5张图片

可下载exe文件

CSDN:https://download.csdn.net/download/soul_mingling/86805199
百度网盘:链接:https://pan.baidu.com/s/1ptq3eAIohD6Ii6pkh6sJxQ
提取码:uk1i

你可能感兴趣的:(瞎搞,python)