我正在尝试在包含 nvidia-smi 输出的 Python 中分析非常大的文本字符串,但我真的想花更多的时间来分析数据,而不是研究我的正则表达式技能。我得到了如下的正则表达式,但它在某些行中需要永远(它可能是某些行中输入数据的变化),但我认为我的正则表达式模式也可能是计算密集型的。
extracted_line1 = r'[=]*[+][=]*[+][=]*\|\n\|(\s+(.*?)\|)+\n\|(\s+(.*?)\|)(\s+(.*?)\|)(\s+(.*?)\|)\n\|'
此模式匹配表中的第三行。
下面这个⬇️
===============================+======================+======================|
| 0 GeForce GTX 1080 On | 00000000:04:00.0 Off | N/A |
| 27% 20C P8 6W / 180W | 2MiB / 8119MiB | 0% E. Process |
| | | N/A |
它适用于大多数行,但随机挂起某些行。这个正则表达式的更简化版本是什么?或者也许更好的问题是获取此表中每一行的每个值(每个 GPU 的相应指标)的最佳方法是什么?
截断的输入字符串在这里
... bunch of text
nvidia-smi:
Tue Jun 8 15:00:02 2021
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 460.80 Driver Version: 460.80 CUDA Version: 11.2 |
|-------------------------------+----------------------+----------------------+
| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |
| | | MIG M. |
|===============================+======================+======================|
| 0 GeForce GTX 1080 On | 00000000:04:00.0 Off | N/A |
| 27% 20C P8 6W / 180W | 2MiB / 8119MiB | 0% E. Process |
| | | N/A |
+-------------------------------+----------------------+----------------------+
| 1 GeForce GTX 1080 On | 00000000:05:00.0 Off | N/A |
| 27% 23C P8 6W / 180W | 2MiB / 8119MiB | 0% E. Process |
| | | N/A |
+-------------------------------+----------------------+----------------------+
... bunch of text
PS我正在尝试提取以下值
gpu_index = [processed result of regex output here]
gpu_model_name = [processed result of regex output here]
persistance_mode = [processed result of regex output here]
bus_id = [processed result of regex output here]
display_active = [processed result of regex output here]
volatile_ecc = [processed result of regex output here]
fan = [processed result of regex output here]
temperature = [processed result of regex output here]
perf = [processed result of regex output here]
power_usage = [processed result of regex output here]
max_power = [processed result of regex output here]
memory_usage = [processed result of regex output here]
available_mem = [processed result of regex output here]
gpu_utilization = [processed result of regex output here]
compute_mode = [processed result of regex output here]
multiple_instance_gpu_mode = [processed result of regex output here]