【python/ros】如何将rosbag的topic数据保存成csv/txt文件
rostopic保存topic数据,bagreader读取topic,open创建文件对象
·
序言
- 记录bag包topic保存的常用方法
topic保存成csv方法1
-
通过命令保存
rostopic echo -b xxx.bag -p /topic_name > xxx.csv
topic保存成csv方法2
-
topic自动提取存储为csv
from bagpy import bagreader import pandas as pd bagPath = r"/your/path/xxx.bag" Bag = bagreader(bagPath) # topics = Bag.topic_table print(topics) # topic内容自动提取到/your/path/xxx(bag包名字)/路径下csv文件 perception_topic = Bag.message_by_topic("/perception/obstacles")
topic保存成csv方法3
-
使用pandas保存,这种方式有点冗余
from bagpy import bagreader import pandas as pd bagPath = r"/your/path/xxx.bag" Bag = bagreader(bagPath) perception_topic = Bag.message_by_topic("/perception/obstacles") topicData = pd.read_csv(perception_topic) directory = '/your/path/' topicData.to_csv(directory + "perception_obstacles.csv") # topicData
topic保存成csv方法4
-
使用csv库保存,可改写保存topic的消息字段
import csv file = open('文件名.csv', 'w', encoding='utf-8') # 基于文件对象构建csv写入对象 csv_writer = csv.writer(file) # 构建列表头 csv_writer.writerow(["姓名","年龄","性别"]) # 写入csv文件内容 csv_writer.writerow(["alice",'21','女']) csv_writer.writerow(["bob",'20','男']) csv_writer.writerow(["clark",'22','男']) f.close()
topic保存成.txt方法1
-
直接使用命令保存
rostopic echo -b xxx.bag -p /topic_name > xxx.txt
topic保存成.txt方法2
-
open()直接创建文件对象
object = {} # object赋值 file = open('/your/path/' + 'perception_obstacles.txt', "w", encoding='utf-8') if object is not None: for target_id in object.keys(): for values in object[target_id]: fields = ' '.join(map(lambda x: str(x), values)) file.write(fields + '\n') file.close()
参考文章:
python写入数据到txt文本中
python读取bag保存到csv
rostopic命令保存csv/txt
created by shuaixio, 2022.06.04
更多推荐
所有评论(0)