0

I have created a function where the user is prompted to enter the quantity and names of the csv files that he wants to open and read.

After checking that the correct filenames have been read and appended to a list I want the function to open each of those files and add each column of the file to a new variable.

files = ['file_a','file_b','file_c','file_d']

These are the variables that will store the feature values of each file.

data1 = []
data2 = []
data3 = []
data4 = []
data5 = []

Then I appended the separate features in each of these variables

for row in file:
        rows = row.split(',')

        try:
            data1.append((rows[0]))
            data2.append(float(rows[1]))
            data3.append(float(rows[2]))
            data4.append(float(rows[3]))
            data5.append(float(rows[4]))
        except IndexError:
            continue

Before that loop closes though I want a new variable created that will hold the data1 to data5 of each csv file separately and can be returned so I can use it further in my script. Is there a way to do that?

apol96
  • 200
  • 12
  • 2
    Does this answer your question? [How do I create a variable number of variables?](https://stackoverflow.com/questions/1373164/how-do-i-create-a-variable-number-of-variables) In this case you need a list of lists. – Pranav Hosangadi Oct 20 '20 at 22:05

2 Answers2

1
all_data = [ data1, data2, data3, data4, data5]

or to keep connected values together, get a list of tuples

all_data = list(zip(data1, data2, data3, data4, data5))
rioV8
  • 24,506
  • 3
  • 32
  • 49
0

Short answer use the exec function:

exec('data1 = [];data2 = []; #etc')

Exec parses strings as python code and can even create variables.

However, what you should is use a list to store the data. I'm using pandas to read CSV, you can do however you wish.

import pandas as pd
files = ['file_a','file_b','file_c','file_d']

data = []
for file in file:
    data.append(pd.read_csv(file))

### acessing data1
do_stuff(data[0])
do_other_stuff(data[1])

This way you code is easily, expandable as it is not dependent on the user inputing exactly the correct input, and you don't need to load an entire string of data(S) for just one line of code. Also it is alot safer, since it stops the user from just writing stuff like ('import sys; sys.exit()') that would crash the program early or write code that would use your program in unintended ways

Eddoasso
  • 386
  • 2
  • 11