【问题标题】:Reading EEPROM addresses in Python and perform operations在 Python 中读取 EEPROM 地址并执行操作
【发布时间】:2018-12-07 15:11:26
【问题描述】:

我目前正在尝试匹配 eeprom 转储文本文件的模式以找到某个地址,然后在搜索中遇到时遍历 4 个步骤。我尝试了以下代码来查找模式

regexp_list = ('A1 B2')
line = open("dump.txt", 'r').read()
pattern = re.compile(regexp_list)

matches = re.findall(pattern,line)

for match in matches:
    print(match)

这会扫描转储中的A1 B2 并在找到时显示。我需要在搜索条件中添加更多这样的地址,例如:'C1 B2', 'D1 F1'。 我尝试将regexp_list 设为列表而不是元组,但没有成功。

这是问题之一。接下来当我遇到搜索时,我想遍历 4 个地方,然后从那里读取地址(见下文)。

输入:

0120   86 1B 00 A1  B2 FF 15 A0  05 C2 D1 E4  00 25 04 00 

在这里,当搜索找到A1 B2 模式时,我想移动 4 个位置,即从转储中保存来自 C2 D1 E4 的数据。

预期输出:

C2 D1 E4

我希望解释清楚。

#

感谢@kcorlidy

这是我为删除第一列中的地址而必须输入的最后一段代码。

newtxt = (text.split("A0 05")[1].split()[4:][:5])

for i in newtxt:
    if len(i) > 2:
        newtxt.remove(i)

所以完整的代码看起来像

import re

text = open('dump.txt').read()

regex = r"(A1\s+B2)(\s+\w+){4}((\s+\w{2}(\s\w{4})?){3})"

for ele in re.findall(regex,text,re.MULTILINE):

    print(" ".join([ok for ok in ele[2].split() if len(ok) == 2]))

print(text.split("A1 B2")[1].split()[4:][:5])

#selects the next 5 elements in the array including the address in 1st col
newtxt = (text.split("A1 B2")[1].split()[4:][:5])

for i in newtxt:
    if len(i) > 2:
        newtxt.remove(i)

输入:

0120 86 1B 00 00 C1 FF 15 00 00 A1 B2 00 00 00 00 C2
0130 D1 E4 00 00 FF 04 01 54 00 EB 00 54 89 B8 00 00

输出:

C2 0130 D1 E4 00

C2 D1 E4 00

【问题讨论】:

标签: python regex python-3.x pattern-matching eeprom


【解决方案1】:

使用正则表达式可以提取文本,也可以通过拆分文本来完成。

正则表达式:

  1. (A1\s+B2) 字符串以 A1 + one or more space + B2 开头
  2. (\s+\w+){4}移动4个位置
  3. ((\s+\w+(\s+\w{4})?){3}) 提取3组字符串,组中可能有4个不需要的字符。然后将它们合二为一。

拆分:

注意:如果文本很长或多行,请不要使用这种方式。

  1. text.split("A1 B2")[1] 将文本分成两部分。之后是我们需要的
  2. .split()被空格分割成列表['FF', '15', 'A0', '05', 'C2', 'D1', 'E4', '00', '25', '04', '00']
  3. [4:][:3]移动4个位置,选择前三个

测试代码:

import re

text = """0120   86 1B 00 A1  B2 FF 15 A0  05 C2 D1 E4  00 25 04 00 
0120 86 1B 00 00 C1 FF 15 00 00 A1 B2 00 00 00 00 C2
0130 D1 E4 00 00 FF 04 01 54 00 EB 00 54 89 B8 00 00 """
regex = r"(A1\s+B2)(\s+\w+){4}((\s+\w{2}(\s\w{4})?){3})"

for ele in re.findall(regex,text,re.MULTILINE):
    #remove the string we do not need, such as blankspace, 0123, \n
    print(" ".join([ok for ok in ele[2].split() if len(ok) == 2]))

print( text.split("A1  B2")[1].split()[4:][:3] )

输出

C2 D1 E4
C2 D1 E4
['C2', 'D1', 'E4']

【讨论】:

  • text = open('dump.txt').read() 正则表达式 = r"(A1\s+B2)(\s+\w{2}){4}((\s+\ w{2}){3})" print(re.search(regex, text).group(3).lstrip()) print(text.split("A1 B2")[1].split()[4 :][:3])
  • 我已经修改了您的代码以读取文件并搜索字符串。它运作良好。最佳!但是,这仅在右侧有足够数量的数组元素时才有效。如果我想遍历下一行的数组,我可以这样做吗?在下面看到我的前任:
  • 0120 86 1B 00 00 C1 FF 15 00 00 A1 B2 00 00 00 00 C2
  • 0130 D1 E4 00 00 FF 04 01 54 00 EB 00 54 89 B8 00 00
  • 当我需要从 A1、B2 遍历接下来的四个位置并且如果这个数组中没有四个元素,我需要从下一行的下两个读取。我们可以这样做吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-01-07
  • 1970-01-01
  • 2018-09-08
  • 2020-08-27
  • 2013-06-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多