0

I'm trying to get a Numpy array of arrays from a csv file Each row is meant to be an independent mumpy array within the general array. (relevant column names are 1,2,3....136).

I've tried this approach but I don't think it's the right way:

with open('faces_new.csv', 'r') as csvfile:
    facesreader = list(csv.DictReader(csvfile, delimiter=',', quotechar='|'))
    main_array = np.array()
    for row in facesreader:
        #create new array inside the main array
        local_array = np.array()
        for n in range(136):
            local_array = np.append(float(row[str(n)]))
            #add n to the array
        main_array = np.append(local_array)
    print(main_array)
Tom Bar-Gal
  • 209
  • 2
  • 4
  • 13
  • Have you seen https://stackoverflow.com/questions/3518778/how-to-read-csv-into-record-array-in-numpy? and/or https://stackoverflow.com/questions/25614749/how-to-import-csv-file-as-numpy-array-in-python/25614793? – Mazdak Jun 17 '18 at 11:33
  • Yes, they are using a different data structure than mine. While I'm only using certain columns (1,2,3...136), they are using all of them, and each row is on a separate array, they are using all data. – Tom Bar-Gal Jun 17 '18 at 11:50
  • 1
    Have you tried first doing normal list and then numpy.array()? Like in here: https://stackoverflow.com/a/31250215/9513536 – Carles Jun 17 '18 at 12:00
  • Definitely not the right way. `np.array()` raises an error. The use of `np.append` is all wrong too. But if you insist on working this way, stick with list and list append. – hpaulj Jun 17 '18 at 15:39
  • 1
    `np.genfromtxt` works with normal `csv` files. If there's something unusual about your file you need to give us a sample. – hpaulj Jun 17 '18 at 15:40

0 Answers0