Skip to content

Instantly share code, notes, and snippets.

@orisano
Created May 30, 2019 08:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save orisano/423b7985bca749365c7675a77b1744fc to your computer and use it in GitHub Desktop.
Save orisano/423b7985bca749365c7675a77b1744fc to your computer and use it in GitHub Desktop.
import json
import sys
head = sys.stdin.readline().strip()
cols = [x for x in head.split(" ") if x]
col_end = {col: head.find(col)+len(col) for col in cols}
def get_col(line, c):
if c == cols[0]:
if line[0] == " ":
return None
return line[:line.find(" ")]
if c == cols[-1]:
offset = col_end[c] - len(c)
if offset >= len(line) or line[offset] == " ":
return None
return line[offset:].strip()
if c in col_end:
x = line[:col_end[c]+1]
if x[-2]== " ":
return None
else:
return x[x.strip().rfind(" ")+1:].strip()
if len(sys.argv) == 1:
print(head)
else:
selected = sys.argv[1:]
for x in sys.stdin:
row = {col: get_col(x, col) for col in selected}
print(json.dumps(row))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment