Skip to content

Instantly share code, notes, and snippets.

@snosrap
Created April 16, 2011 20:30
Show Gist options
  • Save snosrap/923468 to your computer and use it in GitHub Desktop.
Save snosrap/923468 to your computer and use it in GitHub Desktop.
Takes a CSV through STDIN and outputs a corresponding Entity-Attribute-Value (EAV) CSV on STDOUT
#!/usr/bin/python
import sys, csv
def main():
csvIn = csv.reader(sys.stdin, delimiter=',', quotechar='"')
csvOut = csv.writer(sys.stdout)
for rowNum, row in enumerate(csvIn):
if rowNum == 0:
headers = row
else:
for columnNum, cell in enumerate(row):
if columnNum == 0:
entity = cell
elif cell:
csvOut.writerow([entity, headers[columnNum], cell])
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment