加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python-从日志文件中提取特定单词(不是关键字)

发布时间:2020-12-17 17:36:36 所属栏目:Python 来源:网络整理
导读:我正在尝试从下面的sample.txt中提取一些单词(如预期的输出所示)并将它们放在列表中.我在提取正确字段方面面临困难.我已经尝试了我的方法,但是在大多数情况下不起作用.我更喜欢使用python来执行此操作,但是可以使用其他语言.任何其他方法的指针都将受到赞赏.

我正在尝试从下面的sample.txt中提取一些单词(如预期的输出所示)并将它们放在列表中.我在提取正确字段方面面临困难.我已经尝试了我的方法,但是在大多数情况下不起作用.我更喜欢使用python来执行此操作,但是可以使用其他语言.任何其他方法的指针都将受到赞赏.

sample.log

//*********************************************************************************
// update section
//*********************************************************************************
      for (i=0; i< models; i = i+1) begin:modelgen

     model_ip model_inst
         (
          .model_powerdown(model_powerdown),.mcg(model_powerdown),.lambda(_lambda[i])
          );
      assign fnl_verifier_lock = (tx_ready & rx_ready) ? &verifier_lock :1'b0;

   native_my_ip native_my_inst
     (
      .tx_analogreset(tx_analogreset),//.unused_tx_parallel_data({1536{1'b0}})

      );

   // END Section I : 
   //*********************************************************************************
   resync 
     #(
       .INIT_VALUE (1)
       ) inst_reset_sync 
       (
    .clk    (tx_coreclkin),.reset  (!tx_ready),// tx_digitalreset from reset 
    .d      (1'b0),.q      (srst_tx_common  )
    );

预期产量

model_ip
native_my_ip
resync

我的尝试

import re

input_file = open("sample.log","r")
result = []
for line in input_file:
    # need a more generic match condition to extract expected results 
    match_instantiation = re.match(r's(.*) ([a-zA-Z_0-9]+) ([a-zA-Z_0-9]+)_inst (.*)',line)


    if match_instantiation:
    print match_instantiation.group(1)
    result.append(match_instantiation.group(1))
    else:
        continue
最佳答案
您可能需要一次阅读多行以确定字符串是否是模块名称
或不.
请尝试以下操作:

import re

input_file = open("sample.log","r")
lines = input_file.read()   # reads all lines and store into a variable
input_file.close()
for m in re.finditer(r'^s*([a-zA-Z_0-9]+)s+([a-zA-Z_0-9]+s+(|#()',lines,re.MULTILINE):
    print m.group(1)

产生:

model_ip
native_my_ip
resync

上面的正则表达式会查找可能的实例名称或#(.

希望这可以帮助.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读