Last active
January 6, 2017 13:15
-
-
Save ei-grad/75783039c44e8e041e0c02aa55b515a3 to your computer and use it in GitHub Desktop.
Tab-separated key=value format parser
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
| 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