Regex to remove new lines up to a specific character

后端 未结 5 1199
情歌与酒
情歌与酒 2021-01-27 14:41

I have a series of strings in a file of the format:

>HEADER_Text1
Information here, yada yada yada
Some more information here, yada yada yada
Even some more i         


        
5条回答
  •  旧时难觅i
    2021-01-27 14:56

    you don't have to use regex:

    [ x.startswith('>') and x or x.replace('\n','') for x in f.readlines()]    
    

    should work.

    In [43]: f=open('test.txt')
    
    In [44]: contents=[ x.startswith('>') and x or x.replace('\n','') for x in f.readlines()]                                                                                   
    
    In [45]: contents
    Out[45]: 
    ['>HEADER_Text1\n',
     'Information here, yada yada yada',
     'Some more information here, yada yada yada',
     'Even some more information here, yada yada yada',
     '>HEADER_Text2\n',
     'Information here, yada yada yada',
     'Some more information here, yada yada yada',
     'Even some more information here, yada yada yada',
     '>HEADER_Text3\n',
     'Information here, yada yada yada',
     'Some more information here, yada yada yada',
     'Even some more information here, yada yada yada']
    

提交回复
热议问题