Searching for text
Simon Forman
rogue_pedro at yahoo.com
Mon Aug 28 23:51:52 EDT 2006
robinsiebler wrote:
> The other thing I failed to mention is that I need to ensure that I
> find the fsType *before* I find the next FontName.
Given these requirements, I'd formulate the script something like this:
f = open(filename)
NUM_LINES_BETWEEN = 7
Fo = '/FontName /ACaslonPro-Semibold'
FS = '/FSType 8'
def checkfile(f):
# Get a (index, line) generator on the file.
G = enumerate(f)
for i, line in G:
# make sure we don't find a FSType
if FS in line:
print 'Found FSType without FontName %i' % i
return False
# Look for FontName.
if Fo in line:
print 'Found FontName at line %i' % i
try:
# Check the next 7 lines for NO FSType
# and NO FontName
n = NUM_LINES_BETWEEN
while n:
i, line = G.next()
if FS in line:
print 'Found FSType prematurely at %i' % i
return False
if Fo in line:
print "Found '%s' before '%s' at %i" % \
(Fo, FS, i)
return False
n =- 1
# Make sure there's a FSType.
i, line = G.next()
if FS in line:
print 'Found FSType at %i' % i
elif Fo in line:
print "Found '%s' instead of '%s' at %i" % \
(Fo, FS, i)
return False
else:
print 'FSType not found at %i' % i
return False
except StopIteration:
print 'File ended before FSType found.'
return False
return True
if checkfile(f):
# File passes...
pass
Be sure to close your file object when you're done with it. And you
might want fewer or different print statements.
HTH
Peace,
~Simon
More information about the Python-list
mailing list