2016-07-12 6 views
0

Dies ist meine erste Bash bei der Verwendung von Python, ich mache ein Array von Vätern und Kindern, ich habe ein Dataset in einer .csv-Datei, die ich in Python gehen muss, damit ich später Bring es zu Java Script. Jedoch kommt immer die gleiche Fehlermeldung, die ich unten gesetzt habe. Weiter unten ist mein Skript. Wäre für jeden Rat sehr dankbar!Zu viele Indexfehler treten in Python auf

Gabri

>>> 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 
>>> 

Und dann meine eigentliche Skript:

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) 
+1

was haben Sie dies zu debuggen versucht? Ich würde aus dem Fehler erraten, dass die Variable 'input_2d_array' nicht wirklich 2d ist, versuche es auszudrucken und die' .shape' auszudrucken. –

+0

Hi-was bedeutet "es auszudrucken"? Entschuldigung, ich bin neu hier, mehr Details wären sehr hilfreich. Vielen Dank! –

+0

Ich habe es herausgefunden - ich benutze einen Mac und ich musste es als Windows-CSV speichern, nicht der Standard-Mac! –

Antwort

0

Die zu viele Indizes Fehler zeigt an, dass input_2d_array kein zwei 2D-Array ist. genfromtxt() gibt nicht zurück, was Sie erwarten.

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

+0

Danke, ich verstehe nicht ganz, was das aber bedeutet? –

+1

Ich habe es herausgefunden - ich benutze einen Mac und ich musste es als Windows-CSV speichern, nicht als Standard-Mac! –