pythonparsinggenfromtxt

reading a table that might have a single row or column (python)


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?


Solution

  • 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