如何编写一个python代码来读取" .bag"在包文件运行时中写入和写入数据

时间:2017-10-24 19:28:04

标签: python python-3.x bag

我已经完成了以下代码来从.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

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

3 个答案:

答案 0 :(得分:1)

在Python 3 saver = tf.train.Saver() sess = tf.Session() saver.restore(sess = sess, save_path='sentiment_analysis_tsm') 中使用您的环境来选择合适的编码。如果您确定,使用utf-8编码的文件可以忽略

的无效字节序列
open()

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

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

此外,with open('/path/to/file', 'r', encoding='needed_encoding') as f: print(f.read()) 内置的文档可能是useful

答案 1 :(得分:0)

来自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()

答案 2 :(得分:0)

您可以使用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')

但是,该软件包似乎仍在开发中。