Search code examples
pythonlistiterable

How should I put these lists together?


I am creating an interface with Pyqt. This interface generates a .txt file.

Basically in text_file, I generate the .txt file, where the title of the file is linked to radio_value and the content of the file in clickconector. The latter is where I have my problem. relleno is a list where is what is filled in the form, and form_label the labels of this. I need to join both lists into one, I tried from the most basic: a = zip (form_label, Relleno), but it gives an error: TypeError: can only join an iterable (I know it has to do with the join of a text file), and many others that basically gives the same error and I need to write that correctly (I know that the return of the code is wrong, it will only return the first tuple. I try to repeat that but with all the tuples)

I'll skip the part where I configure buttons to be more specific. Maybe It's a rookie mistake, but I've tried for hours and can't get what I want. Any constructive criticism is welcome

def clickconector (self):
    relleno = [self.bitacora.text(), self.turno.text(), self.asesor.text(), self.item.text(), self.modelo.text(), self.identificacion.text(), self.rig.text(),self.horometro.text(),self.condicion.text(),self.orden.text(), self.observacion.text()]
    form_label = ["bitacora", 'turno', 'asesor', 'item', 'modelo', 'identificacion', 'rig', 'horometro', 'condicion', 'orden', 'observacion']
    for a,b in zip (form_label, relleno):
        print (a,b)
        
        
def radio_value (self):
    if self.pendiente.isChecked():     
        return 'Pendiente' , self.bitacora.text()
    
    if self.terminado.isChecked():
        return 'Terminado', self.bitacora.text()

def text_file(self):
    filename = f"{' '.join(self.radio_value())}.txt"
    with open(filename, "w") as f:
        f.write(" ".join(self.clickconector()))
        return f

Solution

  • Your error comes from the line

        f.write(" ".join(self.clickconector()))
    

    clickconector() doesn't return anything thus the error says only iterable~.

    Try returning some text in iterable or just the text.

    ----- update -----

    I'll change it like this

    def clickconector (self):
        relleno = [self.bitacora.text(), self.turno.text(), self.asesor.text(), self.item.text(), self.modelo.text(), self.identificacion.text(), self.rig.text(),self.horometro.text(),self.condicion.text(),self.orden.text(), self.observacion.text()]
        form_label = ["bitacora", 'turno', 'asesor', 'item', 'modelo', 'identificacion', 'rig', 'horometro', 'condicion', 'orden', 'observacion']
        return [str(label_text) for label_text in zip(form_label, relleno)]  # just return list of tuples
           
            
    def radio_value (self):
        if self.pendiente.isChecked():     
            return 'Pendiente' , self.bitacora.text()
        
        if self.terminado.isChecked():
            return 'Terminado', self.bitacora.text()
    
    def text_file(self):
        filename = f"{' '.join(self.radio_value())}.txt"
        with open(filename, "w") as f:
            f.write(" ".join(self.clickconector()))
            return f