ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Cannot save data Callback function

asked 2019-04-09 07:17:00 -0500

Tayssir Boubaker gravatar image

updated 2019-04-09 11:25:23 -0500

What I am really looking for is a way to save all value's message that a node receives (in a list, CSV file..) in a way that I can later do something with this values.

So this is data received :

image description

I tried to put all these values in myData List:

def callback(data):
    id =
    x = data.pose.x
    y = data.pose.y

    #print data
    #print id ,x,y
    #print goals
    print myData

but it's not what I want(Even with append) ! it put every value in a list like this :

image description

Also, I tried to save this value in a CSV File :

with open('goals_point.csv', 'with') as csvFile:
       writer = csv.writer(csvFile)

it saves only the last value :

image description

So how can I save collected messages (data)? Any idea?

Thanks for answering and for your time in advance,


edit retag flag offensive close merge delete


Please don't use an image to display text. Images are not searchable and people cannot copy and paste the text from the image. Please see the support page

jayess gravatar image jayess  ( 2019-04-09 12:14:56 -0500 )edit

2 Answers

Sort by » oldest newest most voted

answered 2019-04-09 09:59:12 -0500

You could save the data into a text file, and import this to excel ( or similar ).

file = open("yourfile.txt", "w+")
data_to_save = str(data.pose.x) + "\t" + str(data.pose.y) + "\n"

With this code above you'll be able to create a multi-lines into your file. However, if you call the same program again the data will be overwritten.

If you need:

edit flag offensive delete link more


thank you, I try it but it saves only the last value,

a = open('goals.txt', 'w+')
    data_to_save = str(data.pose.x) + "\t" + str(data.pose.y) + "\n"

output txtfile :

10.0    20.0

I think that i should not put it in my callback. So the problem now is who can I got data.pose out of my callback !??

Tayssir Boubaker gravatar image Tayssir Boubaker  ( 2019-04-09 10:55:29 -0500 )edit

A parte de open('goals.txt', 'w+') você deve colocar em uma parte onde isso não se repita, ou sejá, no começo do programa não pode colocar essa linha dentro do callback, somente a parte de escrever.

Teo Cardoso gravatar image Teo Cardoso  ( 2019-04-09 13:31:40 -0500 )edit

Sorry, You shouldn't put the line:

a = open('goals.txt', 'w+')

inside the callback function, this line must be call once.

The lines which write inside the text file must be in the callback function.

something like this:

a = open('goals.txt', 'w+')

def   callbackFunction():
      data_to_save = str(data.pose.x) + "\t" + str(data.pose.y) + "\n"
Teo Cardoso gravatar image Teo Cardoso  ( 2019-04-09 13:32:35 -0500 )edit

Thanks i got it ;)

Tayssir Boubaker gravatar image Tayssir Boubaker  ( 2019-04-09 15:53:02 -0500 )edit

answered 2019-04-09 08:27:50 -0500

Reamees gravatar image

As I understand your main goal is to save some topic as a csv. If that is the case you can do that without writing a single line of code. You can pipe the output of rostopic echo with the -p flag to some file and get a csv that way, my answer to a similar question.

rostopic echo -p /turtlebot/stamped_transform/transform/translation > xyz_output.csv

will produce a text file xyz_output.csv with the data formated like the example below, you can change the topic to suit your needs.


There is also the ROS utility called rosbag, in case you don't need the data in csv, but just something to record and play back the data.

As for why your code is not working, it's hard to say without seeing the whole code. Also don't post screenshots of your code and terminal outputs. Anything that is text should be in text format in the question, so it would be copy paste-able and searchable.

If you do want your own node to write the CSV then there are some things that seem out of place in the code.

it saves only the last value :

Your code is most probably not saving only the last value, it is creating the same file with different values and only the last iteration of the cycle remains. You are opening the file with the w option, which overwrites the file every time it is called. Opening the file with w is not the cause of your issues though. Instead of the with file opening method you should probably open the file when you are initializing the node and write to it in the callback. You will then need to close the file when you close the node. Handling writing csv files is more of a general programming question that is better suited for a general programming QA forum.

edit flag offensive delete link more


Thanks, I'm absolute beginner and I'm just getting started learning ROS and I have no idea about rosbag but I will try it. Also , I try to redirect my output from terminal to my existing file ..but dosent working :"bash:No such file or directory" Finally,about saving csv file ,how could I access to my data when im out my callback!

Tayssir Boubaker gravatar image Tayssir Boubaker  ( 2019-04-09 11:15:16 -0500 )edit

Question Tools



Asked: 2019-04-09 07:17:00 -0500

Seen: 784 times

Last updated: Apr 09 '19