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!

FaCoffee
  • 7,609
  • 28
  • 99
  • 174
  • 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. – FaCoffee Nov 04 '15 at 10:32

1 Answers1

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]
Will Vousden
  • 32,488
  • 9
  • 84
  • 95
  • 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! – FaCoffee Nov 04 '15 at 15:05
  • 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? – FaCoffee Nov 04 '15 at 15:13
  • 1
    @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). – Will Vousden Nov 04 '15 at 18:07
  • 1
    @FrancescoCastellani Also see this: http://stackoverflow.com/questions/9001509/how-can-i-sort-a-dictionary-by-key – Will Vousden Nov 04 '15 at 18:07
  • 1
    @FrancescoCastellani Alternatively, just use `sorted(arrays)` to get a sorted list of keys, and use them to access the items of `arrays` in order. – Will Vousden Nov 04 '15 at 18:09
  • Where exactly should I call the `OrderedDict` in your example with `dicts`? Should I call it upon assigning `arrays[file]`? – FaCoffee Nov 04 '15 at 19:12
  • To test the last block I created a folder named "prova" in which there are 3 `csv` files: `file_1.csv`, `file_2.csv`, `file_3.csv`. Each file has a single `int` as value: `1`, `2`, and `3` respectively. How come that the script gets stuck at the `for` line and throws out `IOError: file_1.csv not found. `? If it prints the correct file name, then it should have found it... what is wrong with this? – FaCoffee Nov 05 '15 at 10:17
  • @FrancescoCastellani Couldn't say, I'm afraid. From your description it sounds like it should work. – Will Vousden Nov 06 '15 at 08:12