1

I have 109 csv files that I want to import using a function or a for-loop. So far I have imported the files manually using the following function:

import numpy as np 

plain_table1 = np.genfromtxt('File-path\File-name1.csv', delimiter=',')
...
plain_table109 = np.genfromtxt('File-path\File-name109.csv', delimiter=',')

Is there a simpler way to import all the data files without having to type all of them in manually?

2
  • You find yourself in need of lots of variables called like var1, var2, var3, ... varN - you probably should use lists (or higher dimensional arrays) instead. Commented Jun 24, 2018 at 13:37
  • Your question is a specific instance of the general problem in the linked dupe: use a dict. Or maybe a big array but that needs further conditions. (Don't look at the other answers on the linked question ;) Commented Jun 24, 2018 at 13:47

3 Answers 3

2

Using a dictionary comprehension, you can construct a dictionary to store your data, using numeric keys to identify arrays:

import numpy as np 

d = {i: np.genfromtxt(f'File-path\File-name{i}.csv', delimiter=',') \
     for i in range(1, 110)}

Then access, for example, the array in File-name100.csv via d[100].

Sign up to request clarification or add additional context in comments.

Comments

0

In its simplest form, use a for loop and a range (assuming that your file names are really numbered 1 through 109). Inside the loop, you could append to a list:

import numpy as np 

first = 1
last = 109
plain_tables = []

for i in range(first, last + 1):
    plain_tables.append(np.genfromtxt('File-path\File-name%s.csv' % i, delimiter=',')

Note the %s in the string. That's a placeholder. Together with the % operator this can be used to get a variable into a string.

Other features exist in Python to do the same, like list comprehensions:

plain_tables = [np.genfromtxt('File-path\File-name%s.csv' % i, delimiter=',')
                for i in range(first, last + 1)]

Comments

0

This should do the work for any number of files as long as all are '.CSV'

import numpy as np
from os import listdir, path
plain_table = {file: np.genfromtxt(path.join(File-path,file), delimiter=',') for file in  listdir(FolderPath)}
print(len(plain_table)) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.