-1

I am trying to execute the following code

def get_data(full_file_path, **kwargs):

    file_extension = full_file_path.split(".")[-1]

    method = {"data":read_table(full_file_path, **kwargs),
              "csv":read_csv(full_file_path, **kwargs)}
    data = pd.method.get(file_extension)

    return data

I am getting the error:

File "C:\Users\code\src\data\make_dataset.py", line 7, in get_data
    method = {"data":read_table(full_file_path, **kwargs),
NameError: name 'read_table' is not defined

Does someone know if it is possible to choose a method of an object, based on a dictionary?

Thanks in advance.

ps.: yes, I have ìmported pandas as pd.

chthonicdaemon
  • 19,180
  • 2
  • 52
  • 66
ℂybernetician
  • 135
  • 2
  • 10
  • If you have imported `pandas as pd` then you have to refer to the functions as `pd.read_table`. – Aran-Fey May 01 '18 at 08:48
  • Possible duplicate of [Difference between "import X" and "from X import \*"?](https://stackoverflow.com/questions/12270954/difference-between-import-x-and-from-x-import) – Aran-Fey May 01 '18 at 08:50
  • you probably want to prepend a `pd.` before using `pandas` functionality – Uri Goren May 01 '18 at 08:57

1 Answers1

1

References to functions in Python are just like normal names. The problem in your code is that you're actually trying to call the functions in your dictionary definition. I've split the lookup into a couple of steps in this version of your function to make it clear:

def get_data(full_file_path, **kwargs):

    file_extension = full_file_path.split(".")[-1]

    method = {"data": pd.read_table,
              "csv": pd.read_csv}
    read = method[file_extension]

    data = read(full_file_path, **kwargs)

    return data
chthonicdaemon
  • 19,180
  • 2
  • 52
  • 66