0

I'm doing a mapping of data. I have a lot of tables, one of them is "abonnement" for example, here I have multiple columns (id, datemajaudit, etc.).

Example of col dictionary;

{'abonnement': ['id',
'datemajaudit',
  'objversion',
  'profilmajaudit',
  'supprime',
  'userconnected',
  'categorieabonnement',
  'code',
  'isaffichable',
  'isextranetunsubscribed',
  'libelle',
  'media'],
 'abonnement_carte_paiement': ['id',
  'datemajaudit',
  'objversion',

for every table, I created a variable that is named d_{table}, d_abonnement for example that contains some information about every column of the table.

Example of d_constab_assoc_equipement_infos_etat

#out[9]:
#[{1000: 3, 1002: 3}, {1022: 1, 1044: 1, 1059: 1, 1049: 1, 1051: 1, 1061: 1}]

Now I want to put this information on a txt, in this format:

abonnement
        id
            1000: 3, |
            1002: 3, |
        datemajaudit
            1022: 1, 
            1044: 1, 
            1059: 1, 
            1049: 1, 
            1051: 1, 
            1061: 1

I do it with this code :

ab = open("premier.txt","w")
t=[0]
exec(f'''for i, zones in list(col.items())[0:300]:
    print(f'\t%s'%i, file=ab)
    for t, zone in enumerate(zones): 
        print(f'\t\t%s'%zone, file=ab)
        print("\\t\t\t\t")
        for q, v in d_{i}[t].items():
            print("èèèèè", q, "(", v, '),', end = " ", file=ab)
            print("|", file=ab)''')

ab.close()

The issue is that I have some difficulties to call the variable that I created, I want to dynamically call the variable with this name: d_abonnement[1], d_abonnement[2], d_abonnement[3], and want to do it for all tables so also d_constab_assoc_equipement_infos_etat[0], d_constab_assoc_equipement_infos_etat[1], constab_assoc_equipement_infos_etat[2], etc, d_devis[0], d_devis[1], d_devis[2]

But with this part of my code for q, v in d_{i}[t].items(): I have some issues, because it returns me on my file txt all the table, for each table, the good columns, but for all tables I have the values of the first table. In other words I cannot have some other tables names d_constab_assoc_equipement_infos_etat[0], d_devis[0], etc, but only one table name, the first one that turns with the code, and it fullfils the data for all the others.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Does this answer your question? [How do I create variable variables?](https://stackoverflow.com/questions/1373164/how-do-i-create-variable-variables) – Brian61354270 Jun 03 '22 at 14:28
  • Dynamically generating and running code is rarely a good idea, if ever. Security issues aside, runtime generated code is _extremely_ challenging to test and debug. Don't use `exec`. Just use an appropriate data structure, like a list or dictionary. – Brian61354270 Jun 03 '22 at 14:34

0 Answers0