2

There are as many as 1440 files in one directory to be read with Python. File names have a pattern as

HMM_1_1_.csv
HMM_1_2_.csv
HMM_1_3_.csv
HMM_1_4_.csv
...

and for HMM_i_j_.csv, i goes from 1 to 144 and j goes from 1 to 10.

How can I import each of them into a variable named HMM_i_j similar to its original name?

For instance, HMM_140_8_.csv should be imported as variable HMM_140_8.

Hadij
  • 3,661
  • 5
  • 26
  • 48
  • With the other words what you need is to declare a variable **name** `dynamically`. ? – Mihai Alexandru-Ionut Feb 28 '18 at 14:41
  • Possible duplicate of https://stackoverflow.com/questions/1373164/how-do-i-create-a-variable-number-of-variables – Luci Feb 28 '18 at 14:42
  • What have you tried already? Include your non-working code. If you haven't even made any code, then I suggest reviewing the Python docs on CSV reader. For variable assignment, you can load those CSVs into an object which has keys that follow from the indices, e.g. `dict["144_1"]` – tehhowch Feb 28 '18 at 14:42
  • 1
    Possible duplicate of [How do I create a variable number of variables?](https://stackoverflow.com/questions/1373164/how-do-i-create-a-variable-number-of-variables) – Mihai Alexandru-Ionut Feb 28 '18 at 14:43

2 Answers2

4

You can do this by using pandas and a dictionary. Here is the script that would probably do what you want.

In order to access to a specific csv file in python environment, just use i.e csv[HMM_5_7].

import pandas as pd
csv = {}
for i in range(1, 145):
    for j in range(1, 11):
        s = 'HMM_{}_{}'.format(i,j) 
        csv[s] = pd.read_csv(s+'.csv')

Or: (shorter)

d = {}

for i in range(1440):
    s = 'HMM_{}_{}'.format(i//10+1,i%10+1)
    d[s] = pd.read_csv(s+'.csv')

Or a less readable one-liner:

d = {'HMM_{}_{}'.format(i//10+1,i%10+1):
     pd.read_csv('HMM_{}_{}.csv'.format(i//10+1,i%10+1)) for i in range(1440)}
Anton vBR
  • 18,287
  • 5
  • 40
  • 46
kaanyilmaz
  • 306
  • 1
  • 12
1

Instead of putting them in variables with this name, you can create a dictionary where the key is the name minus '_.csv" and the value is the content of the file.

Here are the steps, I let you figure out how to exactly do each step:

  • Create an empty dictionary
  • Loop i from 1 to 144 and j from 1 to 10
  • If the corresponding file exists, read it and put its content in the dictionary at the corresponding key
nyr1o
  • 966
  • 1
  • 9
  • 23