我有一個包含8個複選框的UI。這個想法是,根據哪些被檢查,它將選擇發送到telnet的命令以及返回哪些數據文件。使用python中的複選框來選擇要寫入/導出的文件
目前我只有8個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()
你應該嘗試重構你的區塊 - 它們幾乎完全相同...... – Stephen
@Stephen我不確定'重構你的區塊'是什麼意思。你能否詳細說明那是什麼以及如何做到這一點。也將解決我的問題? – mad5245