Skip to content

Instantly share code, notes, and snippets.

@ei-grad
Last active January 6, 2017 13:15
Show Gist options
  • Save ei-grad/75783039c44e8e041e0c02aa55b515a3 to your computer and use it in GitHub Desktop.
Save ei-grad/75783039c44e8e041e0c02aa55b515a3 to your computer and use it in GitHub Desktop.
Tab-separated key=value format parser
def parse_line(line):
return dict(i.split('=', 1) for i in line.split('\t') if i)
def loads(s):
cur, prev = 0, 0
while True:
cur = s.find("\n", prev)
if cur == -1:
d = parse_line(s[prev:cur])
if d:
yield d
break
yield parse_line(s[prev:cur])
prev = cur + 1
def load(f):
for line in f:
yield parse_line(line[:-1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment