Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何从ROS订阅dict对象?_Python_Ros_Subscriber - Fatal编程技术网

Python 如何从ROS订阅dict对象?

Python 如何从ROS订阅dict对象?,python,ros,subscriber,Python,Ros,Subscriber,如何订阅此dict对象信息? 或者,如果我可以在其他代码中使用此信息,以便获得对象列表,则会更好。 例如,像talker.py和listener.py这样的典型示例,在talker.py中有rospy.loginfo(),并且listener.py具有带有rospy.loginfo(rospy.get\u caller\u id()…)的回调函数。和rospy.loginfo(rospy.get\u caller\u id())从talker.py订阅信息。就像这样,我想从上面的dict对象中获

如何订阅此dict对象信息? 或者,如果我可以在其他代码中使用此信息,以便获得对象列表,则会更好。
例如,像talker.py和listener.py这样的典型示例,在
talker.py
中有
rospy.loginfo()
,并且
listener.py
具有带有
rospy.loginfo(rospy.get\u caller\u id()…)的回调函数。和
rospy.loginfo(rospy.get\u caller\u id())
talker.py
订阅信息。就像这样,我想从上面的dict对象中获取信息,但我不知道怎么做。

您可以这样做,例如,我有一个带有以下代码的_script.py

if __name__ == '__main__':
     rospy.init_node('grounding_node_temp')
     rospy.sleep(1.0)
     pub = rospy.Publisher('/task_commands', String, queue_size=10, latch=True)
     rospy.sleep(1.0)


     d = {"storage_left": ['book', 'eraser', 'soap2'], "storage_right": ['snacks', 'biscuits', 'glue', 'soap'] }

     pub.publish(json.dumps(d, encoding='ascii'))
     rospy.sleep(1.0)
     rospy.spin()
您可以使用
import
语句访问dict

my_dict = {
    'id': 1
}

我也这样做了,但它说'AttributeError:'module'对象没有属性'my_dict'(对于我的情况,'d')。您需要将它们放在同一目录下,如果没有,则需要包含父目录
import.my_script
import my_script

print(my_script.my_dict) # { 'id': 1 }