0

This is my first bash at using python, I am making an array of fathers and children, I have a dataset in a .csv file which I need to go into python so I can later take it over to java script. However the same error message keeps coming which I have put below. Further down is my script. Would be most grateful for any advice!

Gabriella

>>> runfile('/Users/gkountourides/Desktop/FunctionalExperiment/untitled0.py', wdir='/Users/gkountourides/Desktop/FunctionalExperiment')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "//anaconda/lib/python3.5/site-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 714, in runfile
    execfile(filename, namespace)
  File "//anaconda/lib/python3.5/site-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 89, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)
  File "/Users/gkountourides/Desktop/FunctionalExperiment/untitled0.py", line 41, in <module>
    father,child = fc_csv_to_javascript_arrays("/Users/gkountourides/Desktop/FunctionalExperiment/fatherChild.csv")
  File "/Users/gkountourides/Desktop/FunctionalExperiment/untitled0.py", line 38, in fc_csv_to_javascript_arrays
    father_str, child_str = from_fc_array_to_fc_string(full_array)
  File "/Users/gkountourides/Desktop/FunctionalExperiment/untitled0.py", line 30, in from_fc_array_to_fc_string
    father_array = input_2d_array[:,0]
IndexError: too many indices for array
>>> 

And then my actual script:

import glob
import numpy as np

def list_all_jpgs():
    javascript_string = "["
    for filename in glob.glob("*.jpg"):
        javascript_string += '"' + filename + '",'
    javascript_string = javascript_string[0:-1] + "]"
    return javascript_string

def load_into_np_array(input_csv_file):
    loaded_array = np.genfromtxt(input_csv_file, dtype=str, delimiter=",")
    return loaded_array

def from_single_array_to_string(input_1d_array):
    output_string = '['
    for entry in input_1d_array:
        output_string += '"'+str(entry)+'",'
    output_string = output_string[0:-1]+']'
    return output_string

def from_fc_array_to_fc_string(input_2d_array):
    father_array = input_2d_array[:,0]
    child_array = input_2d_array[:,1]
    father_string = from_single_array_to_string(father_array)
    child_string = from_single_array_to_string(child_array)
    return father_string, child_string

def fc_csv_to_javascript_arrays(csv_input):
    full_array = load_into_np_array(csv_input)
    father_str, child_str = from_fc_array_to_fc_string(full_array)
    return father_str, child_str

father,child = fc_csv_to_javascript_arrays("/Users/gkountourides/Desktop/FunctionalExperiment/fatherChild.csv")
print(father)
print(child)
Gabriella
  • 421
  • 3
  • 11
  • 1
    what have you tried to debug this? I'd guess from the error that the variable `input_2d_array` is not actually 2d, try printing it out and printing out its `.shape`. – Tadhg McDonald-Jensen Jul 12 '16 at 17:40
  • Hi-what does 'printing it out mean'? Sorry, I'm new to this, more detail would be really helpful. Thanks! – Gabriella Jul 13 '16 at 10:01
  • Figured it out-I'm using a mac and I needed to save it as a windows csv, not the default mac one! – Gabriella Jul 13 '16 at 10:15
  • What do you mean "default mac one"? The file extension `.csv` is for standard text files and cross compatible across all operating systems. And printing it out means using the [`print` function for python3](https://docs.python.org/3.3/library/functions.html#print) or [`print` statement for python2](). Either way adding `print(input_2d_array) ; print(input_2d_array.shape)` right above the line that fails would show the data to the console before the failure. – Tadhg McDonald-Jensen Jul 13 '16 at 20:27

1 Answers1

0

The too many indices error indicates that input_2d_array is not a two 2d array. genfromtxt() is not returning what you are expecting.

numpy.genfromtxt produces array of what looks like tuples, not a 2D array—why?

http://docs.scipy.org/doc/numpy/reference/generated/numpy.genfromtxt.html

Community
  • 1
  • 1