上面这个回复中,将current
时间: 2024-01-23 20:02:18 浏览: 19
抱歉,我的回答有误。在上一个回答中,current的两个数据的区分是错误的。
以下是正确的代码实现:
```python
import re
# 定义正则表达式
regex = r"frame=(\d+).*time=(\d+).*speed=(-?\d+),(-?\d+).*encoder=:?(\d+).*opf_xy=:?([-+]?\d+\.\d+),([-+]?\d+\.\d+).*squal=(\d+).*sum=(\d+).*yaw=([-+]?\d+\.\d+).*pitch=([-+]?\d+\.\d+).*roll=([-+]?\d+\.\d+).*floor=(-?\d+).*current=(-?\d+,\d+).*uw=(-?\d+)"
# 读取log文件
with open("log.txt", "r") as f:
log = f.read()
# 使用正则表达式匹配变量
matches = re.finditer(regex, log, re.MULTILINE)
# 定义容器
variables = []
for matchNum, match in enumerate(matches, start=1):
# 获取匹配到的变量
frame = match.group(1)
time = match.group(2)
speed_left = match.group(3)
speed_right = match.group(4)
encoder = match.group(5)
opf_xy_x = match.group(6)
opf_xy_y = match.group(7)
squal = match.group(8)
sum = match.group(9)
yaw = match.group(10)
pitch = match.group(11)
roll = match.group(12)
floor = match.group(13)
current = match.group(14)
uw = match.group(15)
# 将变量存储到容器中
variables.append({
"frame": int(frame),
"time": int(time),
"speed": {
"left": int(speed_left),
"right": int(speed_right)
},
"encoder": int(encoder),
"opf_xy": {
"x": float(opf_xy_x),
"y": float(opf_xy_y)
},
"squal": int(squal),
"sum": int(sum),
"yaw": float(yaw),
"pitch": float(pitch),
"roll": float(roll),
"floor": int(floor),
"current": {
"left": int(current.split(",")[0]),
"right": int(current.split(",")[1])
},
"uw": int(uw)
})
# 输出变量
print(variables)
```
这段代码可以读取名为"log.txt"的log文件,并使用正则表达式匹配变量。匹配到的变量将会存储在一个容器中,容器中每个元素都是一个字典,包含了所有匹配到的变量和对应的值。在代码中已经将变量speed、encoder、opf_xy和current的两个数据区分为了left和right。最后将容器打印输出。