从文件中的部分捕获值:Python

时间:2018-04-30 08:39:23

标签: python python-3.x pandas numpy

# line 1 contains some info
# line 1 contains some info
# Creation date: Tue Aug 16 17:13:37 2016

# line 3 contains some info
# line 4 contains some info

DEFAULT_TEMPLATE
{
    DELAY_TIME        30;
    TEMPLATE_FNAME    "ABC";
    VENDOR_DELAYED    "/";
    DELAY_FNAME       "ABC_NAME";
    DELAY_FVALUE      "DELAYED-30";
    DELAY_FMODE       "END";
}

TEMPLATE
{

  2548                # some-details1
  {
    DELAY_TIME        0;
    DELAY_FNAME       "DSPLY_NAME";
    DELAY_FVALUE      "DELAYED-0";
    DELAY_FMODE       "END";
  }

  1                   # some details2
  {
    DELAY_TIME        20;
    DELAY_FNAME       "DSPLY_NAME";
    DELAY_FVALUE      "DELAYED-20";
    DELAY_FMODE       "END";
  }

  2                   # some details3
  {
    DELAY_TIME        20;
    DELAY_FNAME       "DSPLY_NAME";
    DELAY_FVALUE      "DELAYED-20";
    DELAY_FMODE       "END";
  }
}

我有类似上面的文件格式,我想只从" TEMPLATE"部分和每个部分下的"模板"我想获得数字(如2548,1,2),它们位于每个部分的开头,并且为#34; DELAY_TIME" (现在是0,20,20)在python中。

1 个答案:

答案 0 :(得分:0)

您可以使用正则表达式:

<强>实施例

import re
with open(filename, "r") as infile:
    data = infile.read()

template = re.search("^TEMPLATE[^.]*", data, re.M).group()
print(re.findall("\s+(\d+)\s+", template, re.M))
print(re.findall("\s+(\d+)\;", template, re.M))

<强>输出:

['2548', '1', '2']
['0', '20', '20']