# Revision history [back]

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.

%time,field.x,field.y,field.z
1531725938918082952,-354.547943115,-41.9697914124,-33.9759483337
1531725939051723003,-354.530578613,-41.9656219482,-33.9720916748
1531725939181190967,-354.52633667,-41.9511375427,-33.9718017578


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.