How to make new variables using exec() in imported method (python)

get_data.py

import pandas as pd

def get_data(names):
    print('Loading data...')

    for name in names:
        exec(f'{name} = pd.read_csv(\'{name}.csv\')', globals())

    print('Data successfully loaded.)

main.py

from get_data import *

names = ['apple', 'banana', 'tomato']
get_data(names)

apple.head()

I want to do this.

But it is not working T.T

What should I do

Please help me...



Read more here: https://stackoverflow.com/questions/68462992/how-to-make-new-variables-using-exec-in-imported-method-python

Content Attribution

This content was originally published by junglekim at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: