2016-07-12 59 views
0

這是我第一次使用Python的bash,我製作了一個父親和孩子的數組,我有一個.csv文件中的數據集,我需要進入python,以便以後可以把它交給java腳本。然而,下面我提到了同樣的錯誤消息。再往下是我的腳本。將非常感激的任何建議!Python中出現太多的索引錯誤

加布裏埃拉

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

然後我的實際腳本:

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

你嘗試過什麼來調試呢?我從錯誤中猜測,變量'input_2d_array'實際上不是2d,嘗試打印出來並打印出它的'.shape'。 –

+0

Hi-what'打印出來的意思'?對不起,我是新手,更詳細的信息會非常有幫助。謝謝! –

+0

想通了 - 我使用的是Mac,我需要將它保存爲Windows csv,而不是默認的mac! –

回答