Nov-17-2022, 04:53 PM
Hi all!
I am trying to make my code read certain indexes of an excel file depending on the type of configuration it has in the database, for example:
This is the table that im using to store the config
ClaveCliente TipoLayout BienesTransp Descripcion Cantidad ClaveUnidad Unidad MaterialPeligroso ....
23825 B 15 14 13 12 11 10 ....
23825 A 36 40 1 65 9 62 ....
If the user selects the type of layout = A in the file, it will read the columns configured in the table, which is where those values would be found in the excel file.
Im using the usecols parameter to store the columns, but instead of reading those columns in the file is taking only the first 6 columns of the file
This is the python code that im using:
Another option that i saw on internet was to use the column like ["A", "AB", "CD"], but honestly not sure how i can reach that.
Using the name of the columns is not an option because every layout comes with different tittles in the columns.
Can you please give me an idea of how to fix this?
I am trying to make my code read certain indexes of an excel file depending on the type of configuration it has in the database, for example:
This is the table that im using to store the config
ClaveCliente TipoLayout BienesTransp Descripcion Cantidad ClaveUnidad Unidad MaterialPeligroso ....
23825 B 15 14 13 12 11 10 ....
23825 A 36 40 1 65 9 62 ....
If the user selects the type of layout = A in the file, it will read the columns configured in the table, which is where those values would be found in the excel file.
Im using the usecols parameter to store the columns, but instead of reading those columns in the file is taking only the first 6 columns of the file
This is the python code that im using:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
conn = pyodbc.connect( [b]DB CONNECTION.....[ / b] ) cursor3 = conn.cursor() #here i store the TipoLayout that is gonna be used layout = request.POST[ "tipLayoutdropdown" ] #this query returns the columns configured cursor3.execute( """ select BienesTransp, Descripcion, Cantidad, ClaveUnidad, Unidad, MaterialPeligroso, CveMaterialPeligroso, Embalaje, DescripEmbalaje, PesoEnKg, ValorMercancia, Moneda, FraccionArancelaria, UUIDComercioExt, Pedimentos from ForaneosLayout where TipoLayout = '""" + layout + """'""" ) columns = [column[ 0 ] for column in cursor3.description] listaqq1 = [] for i in cursor3.fetchall(): #listaqq1.append(dict(zip(columns, i))) #Aqui trae tanto el nombre de la columna (columns) y el valor (i) dentro de un diccionario #listaqq1.append(dict(zip(columns))) # Aqui trae el nombre de la columna (columns) dentro de un diccionario listaqq1.append(i) # Aqui trae el valor (i) usecols1 = list (listaqq1[ 0 ]) # Load the xlsx file #excel_data = pd.read_excel(excel_file, usecols=usecols1).iloc[:, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]] excel_data = pd.read_excel(excel_file, usecols = usecols1) pdb.set_trace() # Read the values of the file in the dataframe data = pd.DataFrame( excel_data ) |
Using the name of the columns is not an option because every layout comes with different tittles in the columns.
Can you please give me an idea of how to fix this?