Hello! There is data:

s="""#EXTINF:0,FREE8 КАНАЛ udp://@239.255.0.107:1234 #EXTINF:0,Русская ночь udp://@239.255.6.102:1234""" 

Need to get:

  udp://@239.255.0.107:1234 #EXTINF:0,FREE8 КАНАЛ udp://@239.255.6.102:1234 #EXTINF:0,Русская ночь 
  • What's the problem? We split the string with the splitlines method, for example, and then we connect it as necessary. - skegg

2 answers 2

Option with regular expressions:

 import re m = re.findall(r'(#.+)\n?\s*(udp:.+)\n?\s*', s, re.IGNORECASE) for item in m: print "%s %s" % (item[1], item[0]) 

    It's all right? True checks are not done here any ...

     ls = s.split('\n') for i in range(len(ls) / 2): print ls[i*2+1].strip() + " " + ls[i*2].strip() 
    • one more solution was invented x = s.split ("\ n") map (lambda l, r: l.strip () + "" + r.strip (), x [0 :: 2], x [1 :: 2]) - KoVadim pm
    • If you use splitlines, you can get by with strips. - skegg