0

I have a folder with n csv files. They are accessed and read in a for loop like this:

#do stuff
for file in os.listdir(directoryPath):
    if file.endswith(".csv"):
       #do stuff

Outside of the for loop are a number of numpy arrays initialized to zero. Each array has to carry a specific value located in the current csv file.

My question: is it possible to use file, which I assume is a string, as an integer, in order to fill my arrays for instance like this: array1[file]=numpy.genfromtxt(file,delimiter=',')[:,2]?

I am afraid this very line does not work as file cannot be treated like an integer, so how would you deal with this? Thank you!

1
  • Yes, an index. In my view, a[file] should work as a[i] but of course i was not "declared" in the for loop so I got stuck. Commented Nov 4, 2015 at 10:32

1 Answer 1

1

You could use enumerate, which generates tuples that pair the index of an item with the item itself:

for i, file in enumerate(os.listdir(directoryPath)):
    if file.endswith(".csv"):
       array1[i] = numpy.genfromtxt(file, delimiter=',')[:,2]

Or you could store the Numpy arrays in a dictionary that is indexed directly by the associated file name:

arrays = {}
for file in os.listdir(directoryPath):
    if file.endswith(".csv"):
       arrays[file] = numpy.genfromtxt(file, delimiter=',')[:,2]

Or with an OrderedDict:

from collections import OrderedDict

arrays = OrderedDict()
for file in os.listdir(directoryPath):
    if file.endswith(".csv"):
       arrays[file] = numpy.genfromtxt(file, delimiter=',')[:,2]
Sign up to request clarification or add additional context in comments.

8 Comments

I assume the resulting arrays, if created as dicts, will not be sorted smallest to largest in terms of key. It's important for me to have them sorted. Should I create a new for which sorts the dicts or should I make sure their keys are sorted smallest to largest upon filling them? Thanks!
But since the file names are used as keys, isn't it true that if the former are sorted event_SMALLEST to event_LARGEST, the latter will automatically follow this order?
@FrancescoCastellani Python dictionaries are unordered; you can't preserve a given ordering of key/value pairs when storing them in a dictionary. You can use OrderedDict instead, which remembers the order in which you added items (which will be useful if they're sorted already).
@FrancescoCastellani Also see this: stackoverflow.com/questions/9001509/…
@FrancescoCastellani Alternatively, just use sorted(arrays) to get a sorted list of keys, and use them to access the items of arrays in order.
|

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.