对于下面这种内容的log.log文件,如何跳过有ESSteps的行,而只读取剩下行中的Rewards和Total Steps数据,并以Total Steps数据为横坐标、Rewards数据为纵坐标画图:[32m[05-14 13:23:20 MainThread @train.py:373][0m Total Steps: 1299956 Reward: 98.06129722973967 [32m[05-14 13:23:21 MainThread @train.py:373][0m Total Steps: 1299984 Reward: 45.173203134342316 [32m[05-14 13:23:22 MainThread @train.py:373][0m Total Steps: 1300015 Reward: 78.54178229069575 [32m[05-14 13:23:22 MainThread @train.py:380][0m Evaluation over: 1 episodes, Reward: 112.39893016163798 Steps: 34 [32m[05-14 13:23:37 MainThread @train.py:427][0m ESSteps: 1951 Reward: 119.89624252947024 step: 33.425 sigma:0.004996741129169055
时间: 2023-11-27 17:52:55 浏览: 69
可以使用Python的re模块来匹配log.log文件中的每一行,然后跳过包含ESSteps的行,取出剩下行中的Total Steps和Reward数据。代码如下:
```python
import re
import matplotlib.pyplot as plt
steps = []
rewards = []
with open('log.log', 'r') as f:
for line in f:
if "ESSteps" in line:
continue
match = re.search(r'Total Steps: (\d+) Reward: ([\d\.]+)', line)
if match:
steps.append(int(match.group(1)))
rewards.append(float(match.group(2)))
plt.plot(steps, rewards)
plt.xlabel('Total Steps')
plt.ylabel('Rewards')
plt.show()
```
这段代码会读取log.log文件中的每一行,如果包含ESSteps则跳过,否则使用正则表达式匹配出Total Steps和Reward数据,并将它们分别添加到steps和rewards列表中。最后,使用matplotlib库画出横坐标为Total Steps、纵坐标为Rewards的图形。
阅读全文