A more flexible way of doing this could be with Pandas. After you record your data, you can sift through it to gather the information you need, add it to a dataframe, perform any operations on it, and save it to a .csv. For example, lets consider some points.
import rosbag
from geometry_msgs.msg import Point
import pandas as pd
# The bag file should be in the same directory as your terminal
bag = rosbag.Bag('./recorded-data.bag')
topic = '/your_topic'
column_names = ['x', 'y']
df = pd.DataFrame(columns=column_names)
for topic, msg, t in bag.read_messages(topics=topic):
x = msg.x
y = msg.y
df = df.append(
{'x': x,
'y': y},
ignore_index=True
)
df.to_csv('out.csv')
good question