For image files I would stick with binary file operation, it can handle text too. Here is example:
# typical image header
str1 = \
"""BANDS = 1
BAND_STORAGE_TYPE = BAND_SEQUENTIAL
BAND_NAME = "N/A"
LINES = 2240
LINE_SAMPLES = 3840
SAMPLE_TYPE = UNSIGNED_INTEGER
SAMPLE_BITS = 8
END
~~now starts binary image data~~
"""
filename = "mc03.img"
# write test image file
fout = open(filename, "wb")
fout.write(str1)
fout.close()
# read image file line by line
for line in open(filename, "rb"):
if 'END' in line:
break
if 'LINES' in line:
# remove trailing newline
line = line.rstrip('\n')
# extract integer value
print int(line.split('=')[1]) # 2240
bumsfeld
Nearly a Posting Virtuoso
1,445 posts since Jul 2005
Reputation Points: 404
Solved Threads: 184