Created
May 26, 2015 21:50
-
-
Save sjktje/ede7775fdb07f819dd5e to your computer and use it in GitHub Desktop.
This file contains 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
def parse_filename(filename): | |
tune = collections.defaultdict(list) | |
in_header = False | |
with open(filename, 'r') as f: | |
for line in f: | |
line = line.strip() | |
if line == '' or line.startswith('%'): | |
continue | |
if line.startswith('X:'): | |
if tune: | |
yield tune | |
tune.clear() | |
in_header = True | |
if in_header: | |
(key, value) = line.split(':', 1) | |
if key in header_keys: | |
tune[header_keys[key]].append(value.strip()) | |
if key == 'K': | |
in_header = False | |
else: | |
tune['abc'] = line.strip() | |
else: | |
if tune: | |
yield tune | |
for tune in parse_filename('test.abc'): | |
print(json.dumps(tune, sort_keys=True, indent=4)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment