Searching for text

Tim Chase python.list at tim.thechases.com
Mon Aug 28 22:05:43 EDT 2006


> The other thing I failed to mention is that I need to ensure that I
> find the fsType *before* I find the next FontName.

found_fontname = False
font_search = '/FontName /ACaslonPro-Semibold'
type_search = '/FSType 8'
for line in file('foo.txt'):
	if font_search in line:
		if found_fontname:
			print "Uh, oh!"
		else:
			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

and look for it to report "Uh, oh!" where it has found another 
"/FontName /ACaslonPro-Semibold".

You can reduce your font_search to just '/FontName' if that's all 
you care about, or if you just want any '/FontName' inside an 
'/ACaslonPro-SemiBold' block, you can tweak it to be something like

for line in file('foo.txt'):
	if found_fontname and '/FontName' in line:
		print "Uh, oh!"
	if font_search in line:
		found_fontname = True

-tkc






More information about the Python-list mailing list