Searching for text

Tim Chase python.list at tim.thechases.com
Mon Aug 28 20:30:32 EDT 2006


> I want to search the file until I find '/FontName /ACaslonPro-Semibold'
> and then jump forward 7 lines where I expect to find '/FSType 8'.  I
> then want to continue searching from *that* point forward for the next
> FontName/FSType pair.  Unfortunately, I haven't been able to figure out
> how to do this in Python, although I could do it fairly easily in a
> batch file.  Would someone care to enlighten me?

found_fontname = False
font_search = '/FontName /ACaslonPro-Semibold'
type_search = '/FSType 8'
for line in file('foo.txt'):
	if font_search in line:
		found_fontname = True
	if found_fontname and type_search in line:
		print 'doing something with %s' % line
		# reset to look for font_search
		found_fontname = False


or, you could

sed -n '/\/FontName \/ACaslonPro-Semibold/,/\/FSType 8/{/\/FSType 
8/p}'

You omit what you want to do with the results when you find 
them...or what should happen when they both appear on the same 
line (though you hint that they're a couple lines apart, you 
don't define this as a "this is always the case" sort of scenario)

-tkc





More information about the Python-list mailing list