-1

タイトル通りのことをやりたい。したがって、以下に示す次の文字列があり、すべての二重改行を見つけられるようにしたい (以下のように間にスペースがある可能性があります):

input = """4. A drawer locli:ing device for locl@.ing ,t  
     15 tier of draivers, one of which is lock controllecl, comprising

     twc, drawer retainina m--mbe , rs loica@ted at the front of th-. 
     drawer@' oiie acljacept each side of the tier of dra,-wers ar,d
      arranged to 
     overlap the front of the,"""

output = re.finditer('\n[\S+]\n', nameString)?????????????????????

output[0] = "4. A drawer locli:ing device for locl@.ing ,t  
     15 tier of draivers, one of which is lock controllecl, comprising"
output[1] = "twc, drawer retainina m--mbe , rs loica@ted at the front of th-. 
     drawer@' oiie acljacept each side of the tier of dra,-wers ar,d
      arranged to 
     overlap the front of the,"
4

1 に答える 1

1

これを見てください:

>>> data = """4. A drawer locli:ing device for locl@.ing ,t  
         15 tier of draivers, one of which is lock controllecl, comprising

         twc, drawer retainina m--mbe , rs loica@ted at the front of th-. 
         drawer@' oiie acljacept each side of the tier of dra,-wers ar,d
          arranged to 
         overlap the front of the,"""

そして今、正規表現をインポートします:

>>> import re

そして、それを分割します:

>>> r = re.split(r'\n\s*\n', data) # for more than 2 newlines: r'\n[\s\n]*\n'

結果を表示します。

>>> r[0]
'4. A drawer locli:ing device for locl@.ing ,t  \n         15 tier of draivers, one of which is lock controllecl, comprising'
>>> r[1]
"         twc, drawer retainina m--mbe , rs loica@ted at the front of th-. \n         drawer@' oiie acljacept each side of the tier of dra,-wers ar,d\n          arranged to \n         overlap the front of the,"
于 2013-07-18T15:32:06.333 に答える