Reading csv file and cant store every element

Viewed 28
def cargar_datos(ruta_archivo:str)->dict:
    congresista={}
    congresistas=[]
    archivo = open(ruta_archivo,"r",encoding="utf-8")
    archivo.readline()
    nombre =""
    linea = archivo.readline()
    asistencia = {}

    while len(linea) > 0:
        datos = linea.split(",")
        if nombre!= datos[0]:
            if congresista !=False:
                congresistas.append(congresista)
            congresista={}
            congresista["nombre"]= datos[0]
            congresista["movimiento"]=datos[1]
            congresista["circunscripcion"]=datos[2]
            asistencia={}
            asistencia[datos[3]]=datos[4]
            congresista["asistencia"]= asistencia
            nombre=datos[0]
        else:
            asistencia[datos[3]]=datos[4]
            congresista["asistencia"]=asistencia
        linea = archivo.readline()
    
    archivo.close()
    return congresistas

Not all my elements from my CSV Files are loaded. I need to load a list of congresistas which has the values of name and each line has one assistance. I need to remove the /n and my function is skipping some values.

[
    {
        'nombre': 'YEPES MARTINEZ JAIME ARMANDO ', 
        'movimiento': 'PARTIDO DE LA U', 
        'circunscripcion': 'TOLIMA', 
        'asistencia': {
            '20/07/2018': 'ASISTIÓ\n', 
            '26/07/2018': 'ASISTIÓ\n', 
            '01/08/2018': 'ASISTIÓ\n', 
            '08/08/2018': 'ASISTIÓ\n'
        }
    }
]
2 Answers

I found a Linear Anwser for my problem using strip and some conditionals. Check it out

def cargar_datos(ruta_archivo:str)->dict:
congresista={}
congresistas=[]
archivo = open(ruta_archivo,"r",encoding="utf-8")
archivo.readline()
nombre =""
linea = archivo.readline()
asistencia = {}

while len(linea) > 0:
    datos = linea.split(",")
    if nombre!= datos[0]:
       if congresista!={}:
          congresistas.append(congresista)
       congresista={}
       congresista["nombre"]= datos[0]
       congresista["movimiento"]=datos[1]
       congresista["circunscripcion"]=datos[2]
       asistencia={}
       asistencia[datos[3]]=datos[4].strip()
       congresista["asistencia"]= asistencia
       nombre=datos[0]
    else:
       asistencia[datos[3]]=datos[4].strip() 
       congresista["asistencia"]=asistencia
    linea = archivo.readline()
congresistas.append(congresista)   
archivo.close()
return congresistas

This is the CSV File used for the problem

If I understand correctly and you're reading from a csv file with multiple lines that match all values except the last one, which you must store in a dictionary called asistencia, why bother using an if and some variables outside the loop to keep track of where you are, when you can have a double loop that keeps reading the file if the following values continue to match.

congresistas = []
archivo = open(ruta_archivo, "r", encoding="utf-8")
archivo.readline()
linea = archivo.readline()
while len(linea) > 0:
    datos = linea.split(",")
    congresista = {}
    congresista["nombre"] = datos[0]
    congresista["movimiento"] = datos[1]
    congresista["circunscripcion"] = datos[2]
    asistencia = {}
    while len(linea) > 0 and congresista["nombre"] == datos[0]:
        asistencia[datos[3]] = datos[4].strip() # .strip() gets rid of '\n'
        linea = archivo.readline()
        datos = linea.split(",")
    
    congresista["asistencia"] = asistencia
    congresistas.append(congresista)
            
archivo.close()
return congresistas
Related