SoFunction
Updated on 2025-04-08

Detailed explanation of Python's example of parsing cyber record file

Cyber ​​RT is a high-performance, flexible robot operating system open source from Baidu. Cyber ​​record is a tool used in Cyber ​​RT for recording and playback of data. Here is an example of parsing a cyber record file using Python that uses the cyber_py library (Python binding of Cyber ​​RT) to read the record file and print the message information.

1. Environmental preparation

Make sure the Cyber ​​RT development environment is installed andcyber_pyThe library can be used normally.

2. Sample code

The following is based oncyber_py3The library's Cyber ​​Record file parsing sample code supports reading directories or single files, and automatically filters non-Record files:

import argparse
import os
from cyber_py3 import record
from cyber_py3.record import RecordReader, RecordWriter, RecordMessage
 
def parse_record_file(file_path):
    """
     Parse a single Cyber ​​Record file
     """
    try:
        reader = RecordReader(file_path)
        print(f"\n===== Resolving files: {(file_path)} =====")
        print(f"Total number of messages: {reader.get_messagenumber()}")
        print(f"Start time: {reader.get_starttime()}")
        print(f"End time: {reader.get_endtime()}")
        print(f"Channel list: {reader.get_channellist()}\n")
 
        # traverse all messages        for channel_name, msg, datatype, timestamp in reader.read_messages():
            print(f"[aisle] {channel_name}")
            print(f"  Timestamp: {timestamp}")
            print(f"  Data Type: {datatype}")
            print(f"  Message length: {len(msg)} bytes")
            print("-" * 60)
 
    except Exception as e:
        print(f"Parsing files {file_path} fail: {str(e)}")
 
def parse_record_directory(directory):
    """
     Parses all Cyber ​​Record files in the directory
     """
    if not (directory):
        print(f"mistake: {directory} Not a valid directory")
        return
 
    for filename in (directory):
        file_path = (directory, filename)
        
        # Verify whether it is a legal Record file through the file header        if record.is_valid_record_file(file_path):
            parse_record_file(file_path)
        else:
            print(f"Skip non Record document: {filename}")
 
if __name__ == "__main__":
    parser = (description='Cyber ​​Record File Analysis Tool')
    parser.add_argument('path', type=str, help='File path or directory path')
    args = parser.parse_args()
 
    target_path = 
 
    if (target_path):
        if record.is_valid_record_file(target_path):
            parse_record_file(target_path)
        else:
            print(f"mistake: {target_path} Not valid Cyber Record document")
    elif (target_path):
        parse_record_directory(target_path)
    else:
        print(f"mistake: {target_path} Does not exist")

3. Instructions for use

Running example:

# parse a single filepython parse_record.py /path/to/
 
# parse directorypython parse_record.py /path/to/record_dir/

Functional Features:

Automatically verify file validity (pass file header verification)

Display file meta information: number of messages, time range, channel list

Support parsing message header information (channel, timestamp, data type)

Automatically skip invalid files and non-Record files

4. Key implementation instructions

1. File verification:

Binary validation is performed using the record.is_valid_record_file() method

More reliable than simply checking file extensions

2. Message traversal:

reader.read_messages() generator reads messages one by one

Return tuple: (channel_name, message, data_type, timestamp)

3. Performance optimization:

Parse the message content on demand (the current example only reads meta information)

Supports streaming reading of large files (no full content loading into memory)

This is the end of this article about the detailed explanation of Python parsing cyber record files. For more related content on Python parsing cyber record, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!