views:

61

answers:

2

How to split this string ans $$TEXT$$ is the delimiter.

  1.MATCHES$$TEXT$$STRING  
  2.MATCHES $$TEXT$$ STRING   
+5  A: 

string.split('$$TEXT$$') ?

>>> a="1.MATCHES$$TEXT$$STRING"
>>> a.split("$$TEXT$$")
['1.MATCHES', 'STRING']

>>> a="2.MATCHES $$TEXT$$ STRING"
>>> a.split("$$TEXT$$")
['2.MATCHES ', ' STRING']

and:

>>> [x.strip() for x in "2.MATCHES $$TEXT$$ STRING".split("$$TEXT$$")]
['2.MATCHES', 'STRING']
adamk
A: 

You may be interested in the csv module, which is designed for comma-separated files but can be easily modified to use a custom delimiter.

import csv
csv.register_dialect( "myDialect", delimiter = "$$TEXT", <other-options> )
lines = [ "1.MATCHES$$TEXT$$STRING", "2.MATCHES $$TEXT$$ STRING" ]

for row in csv.reader( lines ):
    ...
katrielalex