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.

4
  • With the other words what you need is to declare a variable name dynamically. ? Commented Feb 28, 2018 at 14:41
  • Possible duplicate of stackoverflow.com/questions/1373164/… Commented Feb 28, 2018 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"] Commented Feb 28, 2018 at 14:42
  • 1
    Possible duplicate of How do I create a variable number of variables? Commented Feb 28, 2018 at 14:43

2 Answers 2

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)}
Sign up to request clarification or add additional context in comments.

3 Comments

You don't need pandas, there is a built-in csv module.
@LouisSugy thank you. Just wrote the first thing came to mind.
Thank you, Anton and kaanyilmaz, for the well-written answer.
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.