pythondatabasenaming

How can i name a variable based on a file name?


I have a large number of .csv files full of data. For this type of data file I've written a Python function that reads the file and creates a datatable that has the information in a useful format. This allows me to create a datatable using the command

Datatable1 = DataReader("path/Data1.csv")

However, I now have to start reading a few thousand files like this, and would like to assign an appropriate variable name to each table. Such that, if a file is called "Data1.csv", I end up with a variable Data1 to which the datatable is assigned. How can I do this? This regards a few thousand files without a clear or consistent naming structure.

I run into trouble when trying to name a variable without directly writing the name of that variable in line, and have no idea how to do so.


Solution

  • i would go for a dictionary that would look something like this

    Datatables = {}
    Datatables['Data1.csv'] = DataReader("path/Data1.csv")
    

    that way, you can easily iterate over all DataTables without having to keep track of all the different variable names. but you still can access each dictionary-entry via its name

    if you really want the variable names directly representing the file i.e. Data1_csv, you can go the dark route and use the eval() Function