Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python中的复选框选择要写入/导出的文件_Python_If Statement_Python 3.x_Python 3.3 - Fatal编程技术网

使用python中的复选框选择要写入/导出的文件

使用python中的复选框选择要写入/导出的文件,python,if-statement,python-3.x,python-3.3,Python,If Statement,Python 3.x,Python 3.3,我有一个带有8个复选框的UI。其思想是,根据检查的是哪些命令,它将选择要发送到telnet的命令以及要返回的数据文件 目前我只有8条IF语句。这会导致某些文件在写入时混淆。我认为解决这个问题的方法是一个较长的if语句,它包含了所有可能的组合,但这是很多组合。有没有一种简单的方法使这些语句不会相互覆盖 下面是一些代码: if self.EHbox.isChecked(): tn.write("geh,".encode('ascii') + TS2Dur.en

我有一个带有8个复选框的UI。其思想是,根据检查的是哪些命令,它将选择要发送到telnet的命令以及要返回的数据文件

目前我只有8条IF语句。这会导致某些文件在写入时混淆。我认为解决这个问题的方法是一个较长的if语句,它包含了所有可能的组合,但这是很多组合。有没有一种简单的方法使这些语句不会相互覆盖

下面是一些代码:

        if self.EHbox.isChecked():
            tn.write("geh,".encode('ascii') + TS2Dur.encode() + b"\n\r")
            out_file = open(self.linePATH.text() + date + "_001.csv", "wt")
            out_file.write(tn.read_until(b"EOF").replace(b'\r\n',b'\n').replace(b'ACK',b'').replace(b'EOF',b'').strip().decode())
            out_file.close()            

        if self.AHbox.isChecked():
            tn.write("DAT,".encode('ascii') + TS2Dur.encode() + b"\n\r")
            out_file = open(self.linePATH.text() + date + "_001.csv", "wt")
            out_file.write(tn.read_until(b"EOF").replace(b'\r\n',b'\n').replace(b'ACK',b'').replace(b'EOF',b'').strip().decode())
            out_file.close()

        if self.DHbox.isChecked():
            tn.write("GDH,".encode('ascii') + TS2Dur.encode() + b"\n\r")
            out_file = open(self.linePATH.text() + date + "_001.csv", "wt")
            out_file.write(tn.read_until(b"EOF").replace(b'\r\n',b'\n').replace(b'ACK',b'').replace(b'EOF',b'').strip().decode())
            out_file.close()            

        if self.L1box.isChecked():
            tn.write("gl1,".encode('ascii') + TS2Dur.encode() + b"\n\r")
            out_file = open(self.linePATH.text() + date + "_001.csv", "wt")
            out_file.write(tn.read_until(b"EOF").replace(b'\r\n',b'\n').replace(b'ACK',b'').replace(b'EOF',b'').strip().decode())
            out_file.close()           

        if self.L2box.isChecked():
            tn.write("gl2,".encode('ascii') + TS2Dur.encode() + b"\n\r")
            out_file = open(self.linePATH.text() + date + "_001.csv", "wt")
            out_file.write(tn.read_until(b"EOF").replace(b'\r\n',b'\n').replace(b'ACK',b'').replace(b'EOF',b'').strip().decode())
            out_file.close()            

        if self.CMbox.isChecked():
            tn.write("gsf,0".encode('ascii') + b"\n\r")
            out_file = open(self.linePATH.text(), "wt")
            out_file.write(tn.read_until(b"EOF").replace(b'\r\n',b'\n').replace(b'ACK',b'').replace(b'EOF',b'').strip().decode())
            out_file.close()            

        if self.CNbox.isChecked():
            tn.write("gsf,1".encode('ascii') + b"\n\r")
            out_file = open(self.linePATH.text(), "wt")
            out_file.write(tn.read_until(b"EOF").replace(b'\r\n',b'\n').replace(b'ACK',b'').replace(b'EOF',b'').strip().decode())
            out_file.close()            

        if self.FLbox.isChecked():
            tn.write("gsf,2".encode('ascii') + b"\n\r")
            out_file = open(self.linePATH.text(), "wt")
            out_file.write(tn.read_until(b"EOF").replace(b'\r\n',b'\n').replace(b'ACK',b'').replace(b'EOF',b'').strip().decode())
            out_file.close()

我会这样做的

首先,我将有一个定义来返回被击中的开关。我要做一个4开关的例子,因为它和8开关一样,只是打字更少

创建每个按钮状态的列表(例如:
[True,False,False,True]
=第一次,第四次开关点击)

然后在需要“做”的地方进行某种形式的For循环。。可能在你点击“开始”按钮之后

从这里,您可以使用lil if语句修改
doButtonJunk()
,查看它是否需要“+date+”\u 001.cvs”部分。。。可能是这样做的

def doButtonJunk(THEDIFFERENCEBETWEENYOUR8THINGS)
        wt = 'wt' # made new placeholder for it
        if THEDIFFERENCEBETWEENYOUR8THINGS in ['gl2,','GDH,'......]: #i diddnt finish this list, because im lazy.. either way its gonna check to see if it needs the date+ part, and then modify the 'wt' to be what it suppsoed to be.
             wt = date+'_001.csv'+wt 
        tn.write(THEDIFFERENCEBETWEENYOUR8THINGS.encode('ascii') + b"\n\r")
        out_file = open(self.linePATH.text(), wt) #inserted wt instead of 'wt' because we defined it now
        out_file.write(tn.read_until(b"EOF").replace(b'\r\n',b'\n').replace(b'ACK',b'').replace(b'EOF',b'').strip().decode())
        out_file.close()

你应该试着重构你的块-它们几乎都是一样的…@Stephen我不知道你说的“重构你的块”是什么意思。你能详细说明一下那是什么以及我怎么做吗。这也能解决我的问题吗?这很有道理。还有很多事情要做,我想我认为有一个简单的方法来处理这个。。。
def clickGo():
    buttons = ['geh,','DAT,','GDH,','gl1,']
    for item in xrange[4]: #<4 is total number of switches
        if checkSwitches()[item]: #checking for Trues ( which im assuming 'isChecked()' returns)
             doButtonJunk(buttons[item])
def doButtonJunk(THEDIFFERENCEBETWEENYOUR8THINGS)
        tn.write(THEDIFFERENCEBETWEENYOUR8THINGS.encode('ascii') + b"\n\r")
        out_file = open(self.linePATH.text(), "wt")
        out_file.write(tn.read_until(b"EOF").replace(b'\r\n',b'\n').replace(b'ACK',b'').replace(b'EOF',b'').strip().decode())
        out_file.close()
def doButtonJunk(THEDIFFERENCEBETWEENYOUR8THINGS)
        wt = 'wt' # made new placeholder for it
        if THEDIFFERENCEBETWEENYOUR8THINGS in ['gl2,','GDH,'......]: #i diddnt finish this list, because im lazy.. either way its gonna check to see if it needs the date+ part, and then modify the 'wt' to be what it suppsoed to be.
             wt = date+'_001.csv'+wt 
        tn.write(THEDIFFERENCEBETWEENYOUR8THINGS.encode('ascii') + b"\n\r")
        out_file = open(self.linePATH.text(), wt) #inserted wt instead of 'wt' because we defined it now
        out_file.write(tn.read_until(b"EOF").replace(b'\r\n',b'\n').replace(b'ACK',b'').replace(b'EOF',b'').strip().decode())
        out_file.close()