How to generate lists named after an item from another list?

Asked

Viewed 71 times

0

It is a program that reads Excel spreadsheet with debit and credit releases on an account.

abreexcel2.py

def leplanilha():
   wb1 = oxl.load_workbook('extratomar2018.xlsx')
   folhas=wb1.get_sheet_names()
   print(folhas)
   sheet=wb1.get_sheet_by_name('plan1')
   nr=sheet.max_row
   nc=sheet.max_column
   for rw in range(2,nr+1):
      if sheet['D'+str(rw)].value in lista:
         pass
      else:
         lista.append(sheet['D'+str(rw)].value)
   print(lista)
leplanilha():

Generated the following list:

['TEDSALARIO', 'APLIC POUP', 'CP MAESTRO', 'DOC ELET', 'DEB.IOF', 'SAQUE B24H', 'PAG FONE', 'PG PREFEIT', 'PAG AGUA', 'PAG BOLETO', 'ENVIO TEV', 'DEP CH 24H', 'CRED TED', 'RESG POUP', 'DOC ELET E', 'DEB CESTA', 'CHEQ COMP', 'SAQUE ATM', 'PG LUZ/GAS']

Question: I would like to generate a list automatically, with each of these elements of this list as its name, to fill them with their releases, but I cannot.

Generating a variable named after an item in a list?

1 answer

0

If you want to create the lists with the name in the source code, then you need to create a file and print the new code with the names you want respecting the language syntax.

However, it is simpler to use a dictionary variable, where each name will be a key to a list, for example:

>>> nome_lista = 'SAQUE ATM'
>>> dicionario = {}
>>> dicionario[nome_lista] = [1,2,3]
>>> print (dicionario['SAQUE ATM'])
[1, 2, 3]
  • Thank you very much Gean. I think it best to do as dictionary same.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.