【问题标题】:How to check if a block of lines has a particular keyword using python?如何使用python检查一行行是否具有特定关键字?
【发布时间】:2018-01-12 07:16:25
【问题描述】:

我正在检查一个带有以下命令块的文本文件 -

File start - 
!
interface Vlan100
 description XYZ
 ip vrf forwarding XYZ
 ip address 10.208.56.62 255.255.255.192
!
interface Vlan101
 description ABC
 ip vrf forwarding ABC
 ip address 10.208.55.126 255.255.255.192
 no ip redirects
 no ip unreachables
 no ip proxy-arp
!
File End

我想创建一个 txt 文件,如果在源文件中我得到一个模式 vrf forwarding ABC 输出应该是 interface Vlan101

到目前为止,我已经按照脚本完成了操作,但它只显示了包含该模式的行。

import re
f = open("output_file.txt","w") #output file to be generated
shakes = open("input_file.txt","r") #input file to read
for lines in shakes:
    if re.match("(.*)ABC(.*)",lines):
        f.write(lines)
f.close()

【问题讨论】:

  • 如果你得到模式 vrf forwarding ABC 你要写入文件的输出是'interface Vlan101'?你能解释一下你的输出文件格式吗?也一样?
  • 在你的for循环中,你需要保存接口信息,然后当匹配你期望的字符串时,你可以打印它。

标签: python regex python-2.7 python-requests file-handling


【解决方案1】:

最简单:读取文件,剪切! 所在的位置,然后对于其中的每一个,如果有所需的文本,则获取第一行:

with open("input_file.txt") as r, open("output_file.txt", "w") as w:
    txt = r.read()
    result = [block.strip().split("\n")[0]
              for block in txt.split('!')
              if 'vrf forwarding ABC' in block]
    w.write("\n".join(result))

【讨论】:

    【解决方案2】:

    为了清楚起见,我想您想用“vrf forwarding ABC”替换“interface Vlan101”的任何实例。在这种情况下,我将 test.txt 作为输入文件,将 out.txt 作为输出文件,其中包含所有需要的替换实例。我使用了列表理解——使用列表字符串方法——将“interface Vlan101”的子字符串替换为“vrf forwarding ABC”。

    with open("test.txt") as f:
        lines = f.readlines()
    
    new_lines = [line.replace("interface Vlan101", "vrf forwarding ABC" for line in lines]
    
    with open("out.txt", "w") as f1:
        f1.writelines(new_lines)
    

    希望这会有所帮助。

    【讨论】:

      【解决方案3】:

      如果你只是对界面感兴趣,你也可以关注一下。

      #Read File
      with open('sample.txt', 'r') as f:
          lines = f.readlines()
      
      #Capture 'interfaces'
      interfaces =  [i for i in lines if i.strip().startswith('inter')]
      
      #Write it to a file
      with open('output.txt', 'w') as f:
          f.writelines(interfaces)
      

      【讨论】:

        【解决方案4】:

        使用您的代码,您将逐行浏览文档。

        如果您想解析块(在“!”符号之间),您可以先将块分成几行(但如果它是一个非常大的文档,您可能需要考虑其他内容,因为这会将整个文档读入内存)

        import re
        
        f = open("output_file.txt","w") #output file to be generated
        source = open("input_file.txt","r") #input file to read
        
        lines = "".join(source) #creates a string from the document
        shakes = lines.replace("\n","").replace("! ","\n")
        # remove all newlines and create new ones from "!"-block delimiter
        
        # retrieve all text before "vrf forwarding ABC"
        finds = re.findall("(.*)vrf forwarding ABC",shakes)
        
        # return start of line
        # if the part you want is the same length in all, 
        # then you could use find[:17] instead of 
        # find to get only the beginning. otherwise you need to modify your
        # regex to only take the first 2 words of the line.
        
        for find in finds:
            f.write(find)
        f.close()
        

        或者,如果您想使用每行匹配,您可以执行与上述相同的操作,但不要替换“!”有了新行,你可以把它拆分,然后使用前面的代码逐行去。

        希望这会有所帮助!

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-11-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-12-18
          • 2018-06-15
          • 2019-12-15
          • 1970-01-01
          相关资源
          最近更新 更多