5

I'm trying to use numpy.loadtxt to read the data in a file that looks like this:

## 14 line of header 3 0 36373.7641026 3 1 36373.7641026 3 2 36373.7641026 ... 

And when I give it this:

>>> chunk, power = numpy.loadtxt(bf,skiprows=14,usecols=(1,2),unpack=True) 

Or even this:

>>> power = numpy.loadtxt(bf,skiprows=14,usecols=(2)) 

It says, TypeError: 'int' object is not iterable

I assumed it was because the first two columns were clearly integers not float, but now I'm not even sure which int object it's referring, because it won't even read just the floats. How do I make loadtxt work?

Related: How do I specify the format of multiple columns using dtype = ? I'm having trouble figuring it out via google.

0

2 Answers 2

10

In your second example, the problem is likely usecols=(2). usecols must be a sequence. (2) is the integer 2, not a one-element tuple containing 2, and is likely what the error message is complaining about: loadtxt() is trying to iterate over an int. Use (2,) (or [2] if you prefer).

Sign up to request clarification or add additional context in comments.

3 Comments

usecols is the option specifying which columns to read. See: docs.scipy.org/doc/numpy-1.4.x/reference/generated/…
@LoonUnit: I certainly understand what usecols is, which is why I know it must be a sequence. As I said in my answer, (2) is not a sequence, it is the integer 2.
Okay, friend explained your answer, and it was ultimately correct. usecols=(2,) did work.
1

It's hard to know what's causing the problem in this case, because you haven't given us enough information. Given what you've posted here, your code should work:

>>> with open('beamtest.out', 'r') as f: ... f.readlines() ... ['header 0\n', 'header 1\n', 'header 2\n', 'header 3\n', 'header 4\n', 'header 5\n', 'header 6\n', 'header 7\n', 'header 8\n', 'header 9\n', 'header 10\n', 'header 11\n', 'header 12\n', 'header 13\n', '3 0 36373.7641026\n', '3 1 36373.7641026\n', '3 2 36373.7641026'] >>> chunk, power = numpy.loadtxt('beamtest.out', skiprows=14, usecols=(1,2), unpack=True) >>> chunk array([ 0., 1., 2.]) >>> power array([ 36373.7641026, 36373.7641026, 36373.7641026]) 

Of course, as kindall's answer indicates, your second example will fail because usecols doesn't accept single integers; it requires a sequence. ((1) is just 1 in parenthesis; to create a tuple, you need a comma in there -- (1,).)

Here is an example of how to use dtype to specify the format of multiple columns:

>>> record = numpy.loadtxt('beamtest.out', skiprows=14, usecols=(1, 2), dtype={'names':('chunk', 'power'), 'formats':('i8', 'f8')}) >>> record array([(0, 36373.7641026), (1, 36373.7641026), (2, 36373.7641026)], dtype=[('chunk', '<i8'), ('power', '<f8')]) >>> record['chunk'] array([0, 1, 2]) >>> record['power'] array([ 36373.7641026, 36373.7641026, 36373.7641026]) 

3 Comments

bf is just a filename, bf = "beamtest.out"
@LoonUnit, look at my edit. It at least answers the second part of your question (about dtype). I tested your first example, and it should work perfectly.
yeah, I think I'm going to end up using your answer in the long run.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.