【PX4日志解析报错】pyulog工具解析日志出错
px4的日志文件一直通过pyulog做解析 但是发现有的时候会出现错误,解析不了,使用网站解析也会失败,但是这把数据很重要所以想办法必须解决,报错如下
zzh@zzh-System-Product-Name:~/Desktop/log/log223/log42$ ulog_info -m MESSAGE log_42_2025-2-23-17-08-34.ulg
Traceback (most recent call last):
File "/home/zzh/.local/bin/ulog_info", line 8, in <module>
sys.exit(main())
File "/home/zzh/.local/lib/python3.8/site-packages/pyulog/info.py", line 85, in main
ulog = ULog(ulog_file_name, None, disable_str_exceptions)
File "/home/zzh/.local/lib/python3.8/site-packages/pyulog/core.py", line 138, in __init__
self._load_file(log_file, message_name_filter_list)
File "/home/zzh/.local/lib/python3.8/site-packages/pyulog/core.py", line 823, in _load_file
self._read_file_definitions()
File "/home/zzh/.local/lib/python3.8/site-packages/pyulog/core.py", line 868, in _read_file_definitions
msg_info = self._MessageInfo(data, header, is_info_multiple=True)
File "/home/zzh/.local/lib/python3.8/site-packages/pyulog/core.py", line 562, in __init__
self.value = ULog.parse_string(data[1+key_len:])
File "/home/zzh/.local/lib/python3.8/site-packages/pyulog/core.py", line 93, in parse_string
ret = _parse_string(cstr)
File "/home/zzh/.local/lib/python3.8/site-packages/pyulog/core.py", line 19, in _parse_string
return str(cstr, 'utf-8', errors)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe5 in position 218: unexpected end of data
定位问题,我是用linux,找到运行的包pyulog包,在python3.8下面
打开这个core.py文件
找到报错的这个函数_parse_string
@staticmethod
def parse_string(cstr):
"""
wrapper for _parse_string with
parametrized exception handling
"""
ret = ''
if _RUNNING_PYTHON3 and ULog._disable_str_exceptions:
ret = _parse_string(cstr, 'ignore')
else:
ret = _parse_string(cstr)
return ret
经过deepseek指点,把else后面的分支改成
else:
# 默认使用 'replace' 替换无法解码的字节
ret = _parse_string(cstr, 'replace')
保存后,再次运行pyulog命令,全部解析出来了