1

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

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.

1
  • It is not. His last column contained floats, not strings. I am getting error because my last column contains strings. Commented Oct 10, 2015 at 18:08

2 Answers 2

4

The numpy.loadtxt function has a parameter usecols. From the documentation:

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.]
Sign up to request clarification or add additional context in comments.

5 Comments

Just did that and I'm still getting error: 'TypeError: 'int' object is not iterable'
I think you must be doing something wrong. I added an example which I used to test and I hope this clarifies. I think you may not be passing a tuple or list to the usecols parameter. Try usecols=[0,1].
Right, I see what you mean. However, I am still getting this error I described above. I only need the 17th column so I wrote 'loadtxt = ("Filename", skiprows=1, delimiter=",", usecols=(17))' . Is that wrong?
In python a tuple with a single element needs to be followed by a comma, so you should write usecols=(17,) or just use a list which doesn't have this problem usecols=[17]. You should also know that the 17th column has the column index 16, so maybe you actually need to write usecols=[16].
Finally! Thank you so much! The comma was missing. Have a nice day!
2

This code block reads the file as a "String array"

numpy.loadtxt('input_file.txt', dtype=str, usecols=(1,2,3,4))

where usecols is used to specify which columns need to be read into the numpy array.

output:

array([['MAX', 'Footprint', 'Center-X', 'Center-Y'],
       ['"100-0009"', '"1206', '-', 'CAPACITOR"'],
       ['"100-0009"', '"1206', '-', 'CAPACITOR"'],
       ['"100-0009"', '"1206', '-', 'CAPACITOR"'],
       ['"100-0009"', '"1206', '-', 'CAPACITOR"'],
       ['"100-0009"', '"1206', '-', 'CAPACITOR"'],
       ['"100-0009"', '"1206', '-', 'CAPACITOR"']], 
      dtype='|S10')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.