Created
January 13, 2015 20:00
-
-
Save samdphillips/2ad473b9110ca77c528e to your computer and use it in GitHub Desktop.
Basic kv_extract text utility
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
import re | |
import sys | |
keys = sys.argv[1:] | |
kv_re = re.compile(r'(?P<key>[^ =]+)=(?P<value>\S+)') | |
def strip_comma(s): | |
if s[-1] == ',': | |
return s[:-1] | |
return s | |
for line in sys.stdin: | |
line = line.strip() | |
d = dict([(m.group('key'), strip_comma(m.group('value'))) | |
for m in kv_re.finditer(line)]) | |
vs = [d.get(k, 'None') for k in keys] | |
print '\t'.join(vs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment