Python read text files in numpy array when empty or single line
Question: I am reading from text files with the code below:

    
    
    import numpy as np
    my_data = np.genfromtxt(resultsDirectory+'/Points.txt', delimiter=' ')
    PointX = my_data[:,5]
    PointY = my_data[:,11]
    

My input files are typically like this -

    
    
    ParamA : 0 ParamB : 7 ParamC : 0 ParamD : 1 Result : FAIL Time : 0 Epsilon : 0.5
    ParamA : 0 ParamB : 11 ParamC : 0 ParamD : 1 Result : FAIL Time : 2 Epsilon : 0.5
    ParamA : 0 ParamB : 7 ParamC : 0 ParamD : 3 Result : FAIL Time : 2 Epsilon : 0.25
    ParamA : 0 ParamB : 13 ParamC : 0 ParamD : 1 Result : FAIL Time : 7 Epsilon : 0.25
    ParamA : 0 ParamB : 7 ParamC : 0 ParamD : 4 Result : FAIL Time : 8 Epsilon : 0.125
    ParamA : 0 ParamB : 8 ParamC : 0 ParamD : 2 Result : FAIL Time : 1 Epsilon : 0.125
    ParamA : 0 ParamB : 8 ParamC : 0 ParamD : 3 Result : FAIL Time : 3 Epsilon : 0.125
    ParamA : 0 ParamB : 8 ParamC : 0 ParamD : 4 Result : FAIL Time : 6 Epsilon : 0.125
    ParamA : 0 ParamB : 9 ParamC : 0 ParamD : 2 Result : FAIL Time : 6 Epsilon : 0.125
    ParamA : 0 ParamB : 10 ParamC : 0 ParamD : 2 Result : FAIL Time : 5 Epsilon : 0.125
    ParamA : 0 ParamB : 14 ParamC : 0 ParamD : 1 Result : FAIL Time : 6 Epsilon : 0.125
    

When I extract PointX from this I get

    
    
    PointX = [7 11 7 13 7 8 8 8 9 10 14]
    PointY = [1 1 3 1 4 2 3 4 2 2 1]
    

Now, sometimes my text files contain only a single line or are even empty.

For example, if the text file has only one line, the my_data array is like
this -

    
    
    [  nan   nan   0.    nan   nan   7.    nan   nan   0.    nan   nan   1.
       nan   nan   nan   nan   nan  0.    nan   nan   0.5]
    

In this case, `my_data.shape` returns `(21,)`.

However reading the array `PointX` or `PointY` gives me an error as
`IndexError: invalid index`. I wanted `PointX=[7]` and `PointY=[1]`. Or if the
text file is empty, it should be `PointX=[]` and `PointY=[]`.

How exactly should I solve this problem? Also I need my `PointX` and `PointY`
to be an array in order not to break the code which is dependent on it.

Thank you.


Answer: Unfortunately, `genfromtxt` returns a 1D array if given a file with only one
line, and returns a 2D array if given more than one line. You could handle the
discrepancy by reshaping:

    
    
    import numpy as np
    my_data = np.genfromtxt('data', delimiter=' ')
    if my_data.ndim == 1:
        my_data = my_data.reshape(1, -1)
    PointX = my_data[:,5]
    PointY = my_data[:,11]
    print(PointX)
    print(PointY)
    

yields

    
    
    [ 7.]
    [ 1.]
    

