how to convert a txt file to a array python. "could not convert string to float"
Question: I need to import a array value of a txt file but when i print to see my values
i get this error: "could not convert string to float: vector(0.1013, 0.2395,
1.1926), vector(0.1276, 0.2361, 1.1760), vector(0.13952644965926353,
0.23617897201269641, 1.1760001652770353), vector(0.16723382973344353,
0.23617897201269641, 1.176000165277035" showing only a few values of the
array. Any idea as to what I am doing wrong and what would be the correct way
to do it ?

    
    
    def load_files(self):
    
        dlg = wx.FileDialog(None,message="Choose File",wildcard= 'Points (*.txt; *.csv)|*.txt;*.csv', defaultFile="",style=wx.FD_OPEN|wx.FD_FILE_MUST_EXIST|wx.FD_CHANGE_DIR)
    
        if dlg.ShowModal() == wx.ID_CANCEL:
                print ('buh cancelaste')
    
        else:
    
    
            filename=dlg.GetFilename()
            f = open(filename)
            data = f.read()
            data = np.array(data).astype(float)
            print(data)
    
    
    files = wx.Button(pan, label='Load Coordinates', pos=(x1+158,570), size = (150,40))
    files.Bind(wx.EVT_BUTTON, load_files)
    

txt file.

vector(0.1013, 0.2395, 1.1926), vector(0.1276, 0.2361, 1.1760),
vector(0.13952644965926353, 0.23617897201269641, 1.1760001652770353),
vector(0.16723382973344353, 0.23617897201269641, 1.1760001652770353),
vector(0.18306661834726065, 0.23617897201269641, 1.1760001652770353),
vector(0.21077399842144068, 0.23219954535704859, 1.1760001652770353),
vector(0.22264858988180353, 0.22822011870140083, 1.1760001652770353),
vector(0.23452318134216635, 0.22822011870140083, 1.1760001652770353)

vector(-3.22925576, 0.78085742, 8.2709313 ), vector(0.12270437, 0.29943441,
1.65709467), vector(0.1278586, 0.09019178, 1.24548948), vector(0.25600214,
-0.04258577, 0.6109198)

vector(0.12795994, 0.30532043, 1.6896684 ), vector( 0.13624277, 0.09229906,
1.24548948), vector(0.29656594, -0.08827312, 0.69378916), vector (0.19870717,
-0.09120946, 1.19266453)


Answer: The error message indicates that `data` as passed to `np.array` is one or more
strings of the form

    
    
    vector(0.1013, 0.2395, 1.1926), vector(0.1276, 0.2361, 1.1760), vector(0.13952644965926353, 0.23617897201269641, 1.1760001652770353), vector(0.16723382973344353, 0.23617897201269641, 1.176000165277035
    

One long string. You did nothing to break it up on spaces, commas or even
newlines.

With `astype(float)` you are trying to convert that string to a float, or
sequence of floats. The Python `float(astr)` function works with strings like
`"0.1013"`, not long ones with many numbers. Even if you broke the `data` into
substrings like

    
    
    vector(0.1013, 0.2395, 1.1926)
    

it would have problems, because `vector` means nothing.

You need to do more parsing of those file lines. You need to split them into
lines (e.g. `readline()`), strip off `\n`, split them up into blocks like
`vector()`, and further split that into strings with one number each. In other
words, a list of strings like this will work:

    
    
    In [848]: np.array(['0.1013', '0.2395', '1.1926'])
    Out[848]: 
    array(['0.1013', '0.2395', '1.1926'], 
          dtype='<U6')
    
    In [849]: np.array(['0.1013', '0.2395', '1.1926']).astype(float)
    Out[849]: array([ 0.1013,  0.2395,  1.1926])
    

* * *

Observe what happens when `data` is that 'vector()' string:

    
    
    In [852]: np.array('vector(0.1013, 0.2395, 1.1926)')
    Out[852]: 
    array('vector(0.1013, 0.2395, 1.1926)', 
          dtype='<U30')
    
    In [853]: np.array('vector(0.1013, 0.2395, 1.1926)').astype(float)
    ---------------------------------------------------------------------------
    ValueError                                Traceback (most recent call last)
    <ipython-input-853-413e30b24430> in <module>()
    ----> 1 np.array('vector(0.1013, 0.2395, 1.1926)').astype(float)
    
    ValueError: could not convert string to float: 'vector(0.1013, 0.2395, 1.1926)'
    

`astype(float)` does not search the string(s) for substrings that look like
numbers. Try `float('123 45')` and variations. `float()` needs something that
looks exactly like a float or integer.

