簡體   English   中英

如何使用python從給定文件中提取一行代碼

[英]How to extract a block of lines from given file using python

我有一個這樣的文件

grouping data-rate-parameters {
    description
      "Data rate configuration parameters.";
    reference
      "ITU-T G.997.2 clause 7.2.1.";

    leaf maximum-net-data-rate {
      type bbf-yang:data-rate32;
      default "4294967295";
      description
        "Defines the value of the maximum net data rate (see clause
         11.4.2.2/G.9701).";
      reference
        "ITU-T G.997.2 clause 7.2.1.1 (MAXNDR).";
    }

      leaf psd-level {
        type psd-level;
        description
          "The PSD level of the referenced sub-carrier.";
      }
    }
  }

  grouping line-spectrum-profile {
    description
      "Defines the parameters contained in a line spectrum
       profile.";

    leaf profiles {
      type union {
        type enumeration {
          enum "all" {
            description
              "Used to indicate that all profiles are allowed.";
          }
        }
        type profiles;
      }

在這里,我想提取每個葉子塊。 例如,葉最大凈數據速率塊為

leaf maximum-net-data-rate {
          type bbf-yang:data-rate32;
          default "4294967295";
          description
            "Defines the value of the maximum net data rate (see clause
             11.4.2.2/G.9701).";
          reference
            "ITU-T G.997.2 clause 7.2.1.1 (MAXNDR).";
        }

像這樣我要提取

我嘗試使用此代碼,這里基於大括號('{')的計數,我正在嘗試讀取該塊

with open(r'file.txt','r') as f:
    leaf_part = []
    count = 0
    c = 'psd-level'
    for line in f:
        if 'leaf %s {'%c in line:
                    cur_line=line
                    for line in f:
                        pre_line=cur_line
                        cur_line=line
                        if '{' in pre_line:
                            leaf_part.append(pre_line)
                            count+=1
                        elif '}' in pre_line:
                            leaf_part.append(pre_line)
                            count-=1
                        elif count==0:
                            break
                        else:
                            leaf_part.append(pre_line)

它適用於leaf maximum-net-data-rate但不適用於leaf psd-level

在進行leaf psd-level ,其顯示也超出了框線。

幫助我完成這項任務。

您可以使用正則表達式:

import re
reg = re.compile(r"leaf.+?\{.+?\}", re.DOTALL)
reg.findall(file)

它返回所有匹配塊的數組
如果要搜索特定的葉子名稱,則可以使用format(請記住使用大括號括起來):

leafname = "maximum-net-data-rate"
reg = re.compile(r"leaf\s{0}.+?\{{.+?\}}".format(temp), re.DOTALL)

編輯:對於python 2.7

reg = re.compile(r"leaf\s%s.+?\{.+?\}" %temp, re.DOTALL)

EDIT2:完全錯過了在上一個示例中有嵌套的括號。
該解決方案比簡單的正則表達式要復雜得多,因此您可以考慮使用另一種方法。 不過,仍然可以做到。
首先,您將需要安裝regex模塊,因為內置的re不支持遞歸模式。

pip install regex

第二,這是你的模式

import regex
reg = regex.compile(r"(leaf.*?)({(?>[^\{\}]|(?2))*})", regex.DOTALL)
reg.findall(file)

現在,此模式將返回一個元組列表,因此您可能需要執行以下操作

res = [el[0]+el[1] for el in reg.findall(file)]

這應該給您完整的結果列表。

它只需要在中斷循環中進行簡單的編輯,因為有多個右括號'}',您的計數已經為負,因此您需要使用

elif count<=0:
    break

但它仍在列表中添加多個大括號,因此您可以通過保留左方括號的記錄來進行處理,而我將代碼更改如下:

with open(r'file.txt','r') as f:
    leaf_part = []
    braces_record = []
    count = 0
    c = 'psd-level'
    for line in f:
        if 'leaf %s {'%c in line:
            braces_record.append('{')
            cur_line=line
            for line in f:
                pre_line=cur_line
                cur_line=line
                if '{' in pre_line:
                    braces_record.append('{')
                    leaf_part.append(pre_line)
                    count+=1
                elif '}' in pre_line:
                    try:
                        braces_record.pop()
                        if len(braces_record)>0:
                            leaf_part.append(pre_line)
                    except:
                        pass
                    count-=1
                elif count<=0:
                    break
                elif '}' not in pre_line:
                    leaf_part.append(pre_line)

上面代碼的結果:

  leaf psd-level { type psd-level; description "The PSD level of the referenced sub-carrier."; } 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM