You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python解析AudioDeviceList.log时列数据匹配错误问题求助

音频设备日志解析异常:状态列提取错误

问题描述

编写Python代码解析AudioDeviceList.log日志,目标提取音频设备名称及对应状态,但代码始终从错误列返回值。已尝试过滤无关数据,问题仍存在。

日志样例

Name                                               Status Manufacturer         Present
------
Speakers (Realtek(R) Audio)                        OK     Microsoft               True
Realtek Audio Effects Component                    OK     Realtek                 True
Realtek(R) Audio                                   OK     Realtek                 True
Realtek Audio Universal Service                    OK     Realtek                 True
Intel® Smart Sound Technology for Bluetooth® Audio OK     Intel(R) Corporation    True

现有代码

import os
import re

# 假设read_file_with_autodetect是已实现的读取文件函数
def read_file_with_autodetect(path):
    with open(path, 'r', encoding='utf-8') as f:
        return f.readlines()

machine_folder = "./path/to/your/folder"  # 替换为实际路径

audio_file_path = None
for root, dirs, files in os.walk(machine_folder):
    for file in files:
        if file == "AudioDeviceList.log":
            audio_file_path = os.path.join(root, file)
            break
    if audio_file_path:
        break

if not audio_file_path or not os.path.exists(audio_file_path):
    print(f"AudioDeviceList.log not found in {machine_folder}")
    return {}

lines = read_file_with_autodetect(audio_file_path)
name_status = {}

for line in lines[3:]:
    if "------" in line:  # Skip separator lines
        continue
    parts = re.split(r'\s{2,}', line.strip())  # Split by two or more spaces
    if len(parts) < 2:
        continue
    name = parts[0].strip()
    status = parts[1].strip()

    if "Intel® Smart Sound Technology for Bluetooth® Audio" in name:
        name = "Intel® Smart Sound Technology for Bluetooth® Audio"
        
    elif "AMD-Dynamic Audio Noise Supression" in name:
        name = "AMD-Dynamic Audio Noise Supression"
    name_status[name] = status

问题分析与解决方法

核心问题

  1. 数据起始索引错误:代码从lines[3:]开始遍历,跳过了第一条设备数据(日志中第一条数据在lines[2],lines[0]是标题、lines[1]是分隔线)。
  2. 按空格分割不可靠:使用re.split(r'\s{2,}', line.strip())分割时,若设备名称包含连续空格(或日志列分隔空格数不稳定),会导致列拆分错位,提取到错误的状态值。
  3. 手动修正名称冗余:因分割错误导致名称不完整,被迫手动修正,属于无效的临时处理。

修复后的代码

利用日志的固定列宽特性,通过标题列的位置索引切片提取数据,彻底避免分割错位问题:

import os

def read_file_with_autodetect(path):
    with open(path, 'r', encoding='utf-8') as f:
        return f.readlines()

machine_folder = "./path/to/your/folder"

audio_file_path = None
for root, dirs, files in os.walk(machine_folder):
    for file in files:
        if file == "AudioDeviceList.log":
            audio_file_path = os.path.join(root, file)
            break
    if audio_file_path:
        break

if not audio_file_path or not os.path.exists(audio_file_path):
    print(f"AudioDeviceList.log not found in {machine_folder}")
    return {}

lines = read_file_with_autodetect(audio_file_path)
name_status = {}

# 从标题行获取各列的起始位置
header = lines[0]
status_start = header.index('Status')
manufacturer_start = header.index('Manufacturer')

# 从第3行(索引2)开始遍历数据行
for line in lines[2:]:
    line = line.strip()
    if not line or "------" in line:
        continue
    # 通过切片提取各列内容,避免空格分割的问题
    name = line[:status_start].strip()
    status = line[status_start:manufacturer_start].strip()
    name_status[name] = status

print(name_status)

修复说明

  • 修正数据起始索引:改为lines[2:],确保第一条设备数据被处理。
  • 基于列位置切片:通过标题行中Status和Manufacturer的位置,精准切割出设备名称和状态,不受名称内空格影响。
  • 移除冗余的名称修正逻辑:切片能完整提取长名称,无需手动修正。

内容的提问来源于stack exchange,提问作者Nature Artist

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 05:35:04