繁体   English   中英

如何编写python代码从“.bag”文件中读取数据并在bag文件运行时写入数据

[英]How to write a python code to read data from “.bag” file and write data in bag file runtime

我已经完成了以下代码来从 .bag 文件中读取数据

import os
f = open("/Volumes/aj/VLP16_Points_2017-10-24-11-21-21.bag", 'r')
print (f.read())
f.close()

我收到以下错误

Traceback (most recent call last):
  File "/Users/ajinkyabobade/PycharmProjects/storingfiles/storingimage.py", line 11, in <module>
    print (f.read())
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/codecs.py", line 321, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)

UnicodeDecodeError: 'utf-8' codec can't decode byte 0x88 in position 33: invalid start byte

如何消除此错误? 另外我如何存储数据运行时间(因为正在生成包文件?)

在 Python 3 open()使用您的环境来选择合适的编码。 如果你确定,那个用 utf-8 编码的文件你可以忽略无效的字节序列

with open('/path/to/file', 'r', error='ignore') as f:
    print(f.read())

或者您可以选择正确的编码(如果您的文件不是 utf-8 编码的)

with open('/path/to/file', 'r', encoding='needed_encoding') as f:
    print(f.read())

此外,关于open内置的文档可能很有用

来自https://wiki.ros.org/rosbag/Code%20API

import rosbag
bag = rosbag.Bag('test.bag')
for topic, msg, t in bag.read_messages(topics=['chatter', 'numbers']):
   print(msg)
bag.close()

您可以使用bagpy包在 Python 中读取 .bag 文件。 可以使用pip安装

pip install bagpy

简要文档位于https://jmscslgroup.github.io/bagpy/

以下是示例代码片段:

import bagpy
from bagpy import bagreader

b = bagreader('09-23-59.bag')

# get the list of topics
print(b.topic_table)

# get all the messages of type velocity
velmsgs   = b.vel_data()
veldf = pd.read_csv(velmsgs[0])
plt.plot(veldf['Time'], veldf['linear.x'])

# quickly plot velocities
b.plot_vel(save_fig=True)

# you can animate a timeseries data
bagpy.animate_timeseries(veldf['Time'], veldf['linear.x'], title='Velocity Timeseries Plot')

但是,看起来该软件包仍在开发中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM