Search and get a line in Python

前端 未结 4 1256
暖寄归人
暖寄归人 2021-02-05 03:26

Is there a way to search, from a string, a line containing another string and retrieve the entire line?

For example:

string = 
    qwertyuiop
    asdfghj         


        
4条回答
  •  失恋的感觉
    2021-02-05 04:20

    If you prefer a one-liner:

    matched_lines = [line for line in my_string.split('\n') if "substring" in line]
    

提交回复
热议问题