Skip to content

Instantly share code, notes, and snippets.

@sjktje
Created May 26, 2015 21:50
Show Gist options
  • Save sjktje/ede7775fdb07f819dd5e to your computer and use it in GitHub Desktop.
Save sjktje/ede7775fdb07f819dd5e to your computer and use it in GitHub Desktop.
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