ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | Q&A
Ask Your Question

Formatting ROS data to .csv file

asked 2019-03-12 14:36:59 -0600

carlito898 gravatar image

updated 2019-03-12 18:50:52 -0600

I created a Rosbag of turtlesim data and would like to export the x and y coordinates only to a .csv file. Right now, however the formatting is as such:


using this command to export to a test .csv file: rostopic echo /turtle1/pose | sed -n '/x:/,/y:/p' > ~/Desktop/test.csv

I would like the formatting to be like this:

x y
x y
x y

Does anyone know how I would do this?

edit retag flag offensive close merge delete

3 Answers

Sort by » oldest newest most voted

answered 2019-03-12 16:40:03 -0600

Wolf gravatar image
rostopic echo -p

prints you the data in a csv-like manner.

I guess in your case it'd be something like

rostopic echo -p /turtle1/pose > ~/Desktop/test.csv
edit flag offensive delete link more

answered 2020-11-04 04:16:54 -0600

fvd gravatar image

updated 2020-11-04 04:18:23 -0600

I think dumping code on Q&A sites is frowned upon, but I came across this trying to implement the Python script below. It exports the joint states from a rosbag to a CSV file. Maybe it will be helpful for someone. Changing the relevant lines to export the poses as requested in the original question should be trivial.

#!/usr/bin/env python
import sys
import os
import csv
import math
import rosbag
import rospy
import copy

# Creates CSV files of the robot joint states from a rosbag (for visualization with e.g. pybullet)
# rosrun your_package /root/catkin_ws/bagfiles 20201021_inhand_pose_exp_peg.bag
# ##################

filename = sys.argv[2]
directory = sys.argv[1]
# Read the rosbag file
print("Reading the rosbag file")
if not directory.endswith("/"):
  directory += "/"
extension = ""
if not filename.endswith(".bag"):
  extension = ".bag"
bag = rosbag.Bag(directory + filename + extension)

# Create directory with name filename (without extension)
results_dir = directory + filename[:-4] + "_results"
if not os.path.exists(results_dir):

print("Writing robot joint state data")

with open(results_dir +"/"+filename+'_joint_states.csv', mode='w') as data_file:
  data_writer = csv.writer(data_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
  data_writer.writerow(['time', 'robot_elbow_joint', 'robot_shoulder_lift_joint', 
  'robot_shoulder_pan_joint', 'robot_wrist_1_joint', 'robot_wrist_2_joint', 'robot_wrist_3_joint'])
  # Get the joint states
  for topic, msg, t in bag.read_messages(topics=['/joint_states']):
    if[0] == "robot_elbow_joint":
      p = msg.position
      data_writer.writerow([t, p[0], p[1], p[2], p[3], p[4], p[5]])

print("Finished creating csv file!")
edit flag offensive delete link more


Would be great if you could add it to the wiki/rosbag/Cookbook page.

gvdhoorn gravatar image gvdhoorn  ( 2020-11-04 04:24:33 -0600 )edit

Right, why not. I added it.

fvd gravatar image fvd  ( 2020-11-04 07:39:33 -0600 )edit

answered 2019-03-12 16:35:07 -0600

The way you attempted to produce the desired CSV isn't really a ROS question at all. Rather, that's a Bash/sed question that would be better asked on something like Super User. That said, there are several ROS ways that you could likely get what you want.

  • The rosbag api would allow you to easily open the bag file and then save the data you want in whatever your desired format. A Python script to do what you're looking for would only be a few lines
  • With a combination of the -p and -b args to rostopic echo you could easily produce a CSV file that included all fields in the topic. Then it would be trivial to extract the columns that you are looking for. See this question on SO for some good examples. I've used cut for this purpose many times.
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools



Asked: 2019-03-12 14:36:59 -0600

Seen: 4,046 times

Last updated: Nov 04 '20