I need to iterate over text file and print specific text starting from and end fulfilling condition in python 3

前端 未结 1 1042
攒了一身酷
攒了一身酷 2021-01-16 20:53

example txt file :

vrf X.x.X
hello
!
how are you
!
vrf y.y.y.
hi
!

I want to run through above provided text and print the output starting

相关标签:
1条回答
  • 2021-01-16 21:24

    The solution using re.findall() function:

    import re
    
    with open('lines.txt', 'r') as fh:
        result = re.findall(r'vrf[^!]+(?=!)', fh.read(), re.M)
    
    print(''.join(result))
    

    The output:

    vrf X.x.X
    hello
    vrf y.y.y.
    hi
    
    0 讨论(0)
提交回复
热议问题