0

I'm using genfromtxt to extract a table from a csv file:

myTable = numpy.genfromtxt("myFile.csv", skip_header=1, delimiter=",", dtype=float)

I don't know in advance the size of the table and it may contain a single row or a single column. I expect to get a 2D array. This works well for most scenarios, but if there is a single row or column, I get a 1D array, which messes up my execution.

A related problem is that a file containing "1,2" and a file containing "1\n2" results with the same output [1 2], while what I would expect the output for the first to be [[1 2]] and for the second [[1][2]]

Is there a way to indicate the dimensionality of the expected output in genfromtxt? Alternatively, is there a different utility for reading tables from files?

Shaharg
  • 971
  • 1
  • 11
  • 26

3 Answers3

0

Use readlines() for going line by line (row) into the file. And in each row, use column=len(row.split(';')) to find the number of column of each row.

for row in file.readlines():
     column=len(row.split(';'))

Now you can extract any kinds of information that you want

Reihaneh Kouhi
  • 489
  • 1
  • 7
  • 23
0

Here is the Code that i have written that might help you I'm also new so you might find a better solution then this

import csv
datafile = open(r'C:\Users\Admin\Downloads\a.csv', 'r')
datareader = csv.reader(datafile, delimiter=';')
data = []
for row in datareader:
    print(row)
    for x in row :
        try:
            ls = x.split(",")
            data.append(ls)   
        except:
            data.append(x)  
print (data)
Sachin Rajput
  • 238
  • 7
  • 26
0

You can wrap the file loading in a try..except block using the raise statement so when the loaded table doesn't match the desired dimensions, you get an error message.

import numpy

try:
    myTable = numpy.genfromtxt(("myFile.csv", skip_header=1, delimiter=",", dtype=float)
    if myTable.shape[0] == 1 or myTable.shape[1] == 1:
        raise IndexError("The imported table has 2 or more dimensions!") 
except:
    raise

This way, anytime you try to load a table into memory which has less than 2 dimensions, you get an IndexError with the error message you specified in the raise statement. Now you could wrap that chunk into a function and try to load the table from within the function. Further, I recommend to create a pandas data frame for its versatile and easy possibilities to manipulate data.