TypeError:“对象”类型的参数不可迭代

时间:2020-04-21 19:23:59

标签: python amqp qpid

我已经写了一个我认为简单的python脚本,可以搜索多行输出并匹配特定字符串(即“ grep”)。列出所有没有模式匹配的队列非常简单:

from qmf.console import Session
sess = Session()
broker = sess.addBroker("amqp://guest/guest@localhost")
queues = sess.getObjects(_class="queue", _package="org.apache.qpid.broker")
for q in queues:
  print (q)

运行脚本会产生以下输出(被截断):

pmena@myhost=> python ./queue_stuff.py
org.apache.qpid.broker:queue[0-1-1-0-62] 0-1-1-0-3:queue01
org.apache.qpid.broker:queue[0-1-1-0-55] 0-1-1-0-3:queue02
org.apache.qpid.broker:queue[0-1-1-0-63] 0-1-1-0-3:queue03
org.apache.qpid.broker:queue[0-1-1-0-51] 0-1-1-0-3:queue04
.
.
org.apache.qpid.broker:queue[0-1-1-0-51] 0-1-1-0-3:queue99

但是,当我添加“ if”语句以匹配特定字符串时,就像这样:

from qmf.console import Session
sess = Session()
broker = sess.addBroker("amqp://guest/guest@localhost")
queues = sess.getObjects(_class="queue", _package="org.apache.qpid.broker")
for q in queues:
  if 'queue37' in q:
    print (q)

我收到以下错误:

pmena@myhost=> python ./queue_stuff.py
Traceback (most recent call last):
  File "./queue_stuff.py", line 6, in <module>
    if 'queue37' in q:
TypeError: argument of type 'Object' is not iterable

我觉得这是一个简单的python语法问题,但无法从其他帖子中收集分辨率。

2 个答案:

答案 0 :(得分:0)

似乎q是Object而不是字符串。 您应该检查

for q in queues:
    print (type(q))

如果q是对象,则不能使用if 'queue37' in q

答案 1 :(得分:0)

问题是Queue类没有您可能会怀疑的 iter 方法。需要为for i in object:的类定义该方法才能工作。 This answer讨论了人们使用的各种变通方法,因此您可以查看哪种变通方法最适合您的需求。