English 中文(简体)
numpy.loadtxt 给出“ 不可重复” 错误
原标题:numpy.loadtxt gives "not iterable" error

我试图使用 noumpy.loadttxt 来读取文件里的数据, 这个文件看起来像 :

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

当我给它这个:

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

或甚至这样:

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

它说, < code> TypeError: 内在对象不是可替换的

我以为是因为前两列的整数不是浮的,但现在我甚至不确定它指的是哪个内号,因为它甚至不会只读到浮点数。我如何使 loadtxt 工作?

相关 : 我如何用 < code> dtype =? I 无法通过 Google 找到多个列的格式 。

问题回答

在您的第二个示例中,问题可能是 usecols=(2) usecols 必须是一个序列。 (2) 是整数2,而不是包含2的单元素图普,并且很可能是错误信息所抱怨的错误信息: loadtxt () 正试图在 unt 上插入。使用 (2) (或 [2] )。

很难知道在本案中是什么导致了问题,因为你没有给我们足够的信息。鉴于你在这里张贴的信息,你的代码应该起作用:

>>> with open( beamtest.out ,  r ) as f:
...     f.readlines()
... 
[ header 0
 ,  header 1
 ,  header 2
 ,  header 3
 ,  header 4
 , 
  header 5
 ,  header 6
 ,  header 7
 ,  header 8
 ,  header 9
 , 
  header 10
 ,  header 11
 ,  header 12
 ,  header 13
 , 
  3 0 36373.7641026
 ,  3 1 36373.7641026
 ,  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])

当然,正如>kindall s 答复所示,第二个例子将失败,因为usecols 不接受单整数;它 rel=“nofollown noreferrer”>> >requires a 序列 。 ( (1) 只是括号中的 1 ;要创建一个tuple,你需要在那里有一个逗号 -- (1) 。

下面是如何使用 dtype 来指定多列格式的示例 :

>>> 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])




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...