Delete last column from file with Python
Question: I am basically trying to do the same thing as this guy but with Python: [How
can i delete last column from my text
file](http://stackoverflow.com/questions/26940261/how-can-i-delete-last-
column-from-my-text-file)

How can I remove my last column?

I tried to load the text using `numpy.loadtxt` first, and then just ignore the
final array but it wouldn't load at all because the last column contains
strings, instead of floats.


Answer: The `numpy.loadtxt` function has a parameter `usecols`. From [the
documentation](http://docs.scipy.org/doc/numpy/reference/generated/numpy.loadtxt.html):

    
    
    numpy.loadtxt(
        fname,
        dtype=<type 'float'>,
        comments='#',
        delimiter=None,
        converters=None,
        skiprows=0,
        usecols=None,
        unpack=False,
        ndmin=0
    )
    Load data from a text file.
    ...
    usecols : sequence, optional Which columns to read, with 0 being
        the first. For example, usecols = (1,4,5) will extract the
        2nd, 5th and 6th columns. The default, None, results in all
        columns being read.
    

Of course this presumes you know in advance how many columns are in the file.

For example, given the following file `test.txt`:

    
    
    100 test1
    200 test2
    300 test3
    

Loading with `numpy.loadtxt("test.txt")` produces this error.

    
    
    $ python -c "import numpy as np;np.loadtxt('test.txt')"
    Traceback (most recent call last):
      ...
      items = [conv(val) for (conv, val) in zip(converters, vals)]
    ValueError: could not convert string to float: test1
    

However using the `usecols` parameter works fine:

    
    
    $ python -c "import numpy as np;print np.loadtxt('test.txt', usecols=(0,))"
    [ 100.  200.  300.]
    

