Last active
December 26, 2015 05:39
-
-
Save godber/7102482 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| """ | |
| Sample of parsing PDS Labels into an OrderedDict | |
| """ | |
| from collections import OrderedDict | |
| def parse_label(pds_file): | |
| """ | |
| Parses a PDS Label from a file, returns an ordered dict | |
| """ | |
| label = OrderedDict() | |
| in_block = None | |
| with open(pds_file) as f: | |
| for line in f.readlines(): | |
| line = line.rstrip() # strip off the ^M and any trailing garbage | |
| if '=' in line: | |
| key, value = line.split('=') | |
| key = key.strip() | |
| value = value.strip() | |
| if key in ['OBJECT', 'GROUP']: | |
| in_block = value | |
| label[value] = OrderedDict([('_type', key)]) | |
| elif key in ['END_OBJECT', 'END_GROUP']: | |
| in_block = None | |
| else: | |
| if in_block: | |
| label[in_block][key] = value | |
| else: | |
| label[key] = value | |
| if line.strip() == 'END': | |
| break | |
| return label | |
| def main(pds_files): | |
| import json | |
| for pds_file in pds_files: | |
| r = parse_label(pds_file) | |
| print(json.dumps(r, indent=4)) | |
| if __name__ == '__main__': | |
| pds_files = [ | |
| '/home/godber/Workspace/test_data/I18584006BTR.IMG', | |
| '/home/godber/Workspace/test_data/MER/1P345688456EFFB0EJP2363L2C1.IMG', | |
| ] | |
| main(pds_files) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment