textfile: copy between 2 keywords

alister alister.nospam.ware at ntlworld.com
Thu Sep 10 21:41:09 CEST 2015


On Thu, 10 Sep 2015 12:11:55 -0700, wxjmfauth wrote:

>>>> s = """1, 2, 3, 4
> ... #keyword1 ... 3, 4, 5, 6 ... 2, 3, 4, 5 ... #keyword2 ... 4, 5, 6
> ,7"""
>>>> s[s.find('keyword1') + len('keyword1'):s.find('keyword2') - 1]
> '\n3, 4, 5, 6\n2, 3, 4, 5\n'
>>>> #or s[s.find('keyword1') + len('keyword1') + 1:s.find('keyword2') -
>>>> 2]
> '3, 4, 5, 6\n2, 3, 4, 5'
>>>>

split works well
as a simple 1 liner (well 2 if you include the string setup)

>>>a="crap word1 more crap word1 again word2 still more crap"

>>>a.split('word1',1)[1].split('word2')[0]

' more crap word1 again '



-- 
All bad precedents began as justifiable measures.
		-- Gaius Julius Caesar, quoted in "The Conspiracy of
		   Catiline", by Sallust


More information about the Python-list mailing list