Skip to content

Instantly share code, notes, and snippets.

@samdphillips
Created January 13, 2015 20:00
Show Gist options
  • Save samdphillips/2ad473b9110ca77c528e to your computer and use it in GitHub Desktop.
Save samdphillips/2ad473b9110ca77c528e to your computer and use it in GitHub Desktop.
Basic kv_extract text utility
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