如何访问数组中的内容?

时间:2019-04-16 05:31:41

标签: python ros

嘿,我是python的新手,我正在尝试打印位于数组status_list内的文本消息。

#!/usr/bin/env python
import rospy
from actionlib_msgs.msg import GoalStatusArray

def callback(data):
    print(data.status_list)


def listener():

    # In ROS, nodes are uniquely named. If two nodes with the same
    # name are launched, the previous one is kicked off. The
    # anonymous=True flag means that rospy will choose a unique
    # name for our 'listener' node so that multiple listeners can
    # run simultaneously.
    rospy.init_node('listener', anonymous=True)

    rospy.Subscriber("/move_base/status", GoalStatusArray, callback)

    # spin() simply keeps python from exiting until this node is stopped
    rospy.spin()

if __name__ == '__main__':
    listener()

这是上面代码的输出。

[goal_id: 
  stamp: 
    secs: 44
    nsecs:  46000000
  id: "/move_base-1-44.46000000"
status: 3
text: "Goal reached."]

我如何访问已达到目标的消息?谢谢。

1 个答案:

答案 0 :(得分:1)

您必须尝试从列表索引(如list [0],[1])中进行操作。 这意味着您正在获取第一个索引。

(data.status_list [0] .text)